[Leetcode]1700.Number of Students Unable to Eat Lunch

67 阅读2分钟

Description:

The school cafeteria offers circular and square sandwiches at lunch break, referred to by numbers 0 and 1 respectively. All students stand in a queue. Each student either prefers square or circular sandwiches.

The number of sandwiches in the cafeteria is equal to the number of students. The sandwiches are placed in a stack. At each step:

  • If the student at the front of the queue prefers the sandwich on the top of the stack, they will take it and leave the queue.
  • Otherwise, they will leave it and go to the queue's end.

This continues until none of the queue students want to take the top sandwich and are thus unable to eat.

You are given two integer arrays students and sandwiches where sandwiches[i] is the type of the i​​​​​​th sandwich in the stack (i = 0 is the top of the stack) and students[j] is the preference of the j​​​​​​th student in the initial queue (j = 0 is the front of the queue). Return the number of students that are unable to eat.

 

Example 1:

Input: students = [1,1,0,0], sandwiches = [0,1,0,1]
Output: 0 Explanation:
- Front student leaves the top sandwich and returns to the end of the line making students = [1,0,0,1].
- Front student leaves the top sandwich and returns to the end of the line making students = [0,0,1,1].
- Front student takes the top sandwich and leaves the line making students = [0,1,1] and sandwiches = [1,0,1].
- Front student leaves the top sandwich and returns to the end of the line making students = [1,1,0].
- Front student takes the top sandwich and leaves the line making students = [1,0] and sandwiches = [0,1].
- Front student leaves the top sandwich and returns to the end of the line making students = [0,1].
- Front student takes the top sandwich and leaves the line making students = [1] and sandwiches = [1].
- Front student takes the top sandwich and leaves the line making students = [] and sandwiches = [].
Hence all students are able to eat.

Solution: Editorial Solution

Key Observation: If none of the students in the queue's preference matches the top sandwich, none of the remaining students can eat.

So editorial solution first count the number of students who prefer circle sandwiches and the number of students who prefer another.

Then, it iterate through the available sandwiches in the stack, if the top sandwich is a circle sandwich, we serve it to a student who prefers circle ones by decrementing the number of students who prefer circle sandwiches. If the top sandwich is square, we serve the same strategy.

If the number of students who prefer a certain type of sandwich becomes zero, and the sandwich at the top of the stack is that same type of sandwich, none of the remaining students want that sandwich. We return the number of unservered students, which is the count of the students who prefer the other type of sandwich.

fun countStudents1(students: IntArray, sandwiches: IntArray): Int {
    var circleStudentCount = 0
    var squareStudentCount = 0
    students.forEach {
        if (it == 0) {
            circleStudentCount++
        } else {
            squareStudentCount++
        }
    }

    sandwiches.forEach {
        if (it == 0 && circleStudentCount == 0) {
            return squareStudentCount
        }

        if (it == 1 && squareStudentCount == 0) {
            return circleStudentCount
        }

        if (it == 0) {
            circleStudentCount--
        } else {
            squareStudentCount--
        }
    }
    return 0
}

My first thought is as the description of the question, using a queue and a stack. the situation we should care about is when the queue has traversed once and doesn't find the sandwich, we should break the loop.

fun countStudents(students: IntArray, sandwiches: IntArray): Int {
    val sandwichesQueue = LinkedList<Int>()
    sandwichesQueue.addAll(sandwiches.toMutableList())

    val studentsQueue = LinkedList<Int>()
    studentsQueue.addAll(students.toMutableList())

    while (true) {
        var size = studentsQueue.size
        if (size == 0) {
            break
        }
        while (size > 0) {
            val s = studentsQueue.poll()
            if (s == sandwichesQueue.peek()) {
                sandwichesQueue.poll()
                break
            } else {
                studentsQueue.add(s)
                size--
            }
        }
        // when size == 0, it means we didn't find any sandwich and has traversed all remaining students
        if (size <= 0) {
            break
        }
    }

    return studentsQueue.size
}