LeetCode 1700. Number of Students Unable to Eat Lunch

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.

Example 2:

Input: students = [1,1,1,0,0,1], sandwiches = [1,0,0,0,1,1]
Output: 3

Constraints:

  • 1 <= students.length, sandwiches.length <= 100
  • students.length == sandwiches.length
  • sandwiches[i] is 0 or 1.
  • students[i] is 0 or 1.

这题也不知道跟stack有啥关系,明明是个queue?把所有学生放进一个queue里然后从队头remove,不行就再塞回队尾就行了。但是这里有一个坑,就是这个循环的终止条件,除了queue empty以外还有一种可能性就是剩下的学生在无限循环往队里塞。如何才能判断这种条件呢?还需要额外用一个变量来记录我们连续塞了多少个学生,如果队里所有学生都在连续塞,那就说明该停下了,接下去就要死循环了。

class Solution {
    public int countStudents(int[] students, int[] sandwiches) {
        Queue queue = new LinkedList<>();
        for (int i : students) {
            queue.add(i);
        }

        int i = 0;
        int count = 0;  // how many consecutive students can not eat sandwich
        while (!queue.isEmpty() && count != queue.size()) {
            int top = queue.remove();
            if (top == sandwiches[i]) {
                i++;
                count = 0;
            } else {
                queue.add(top);
                count++;
            }
        }

        return queue.size();
    }
}

然后看了答案发现还有另一种思路,不需要用到queue。其实我们根本不care学生的顺序,而只care一个sandwich有没有学生想吃它。所以我们先拿到分别想吃两种sandwich的学生人数,然后再遍历sandwich数组,看看到第几个sandwich开始没人想吃它,那剩下的学生/sandwich个数就是吃不上sandwich的学生人数了。

class Solution {
    public int countStudents(int[] students, int[] sandwiches) {
        int count0 = 0;
        int count1 = 0;
        for (int i : students) {
            if (i == 0) {
                count0++;
            } else {
                count1++;
            }
        }

        for (int i : sandwiches) {
            if (i == 0) {
                if (count0 == 0) {
                    return count1;
                }
                count0--;
            } else {
                if (count1 == 0) {
                    return count0;
                }
                count1--;
            }
        }
        return 0;
    }
}

还有另一种写法计算剩下的sandwich数的,其实本质都一样:- LeetCode

你可能感兴趣的:(leetcode)