LeetCode 1700. Number of Students Unable to Eat Lunch

文章描述了一个编程问题,涉及到学生对圆形和方形三明治的偏好。学生排队,按顺序选择他们喜欢的三明治,不喜欢的则返回队尾。给定学生偏好和三明治类型,目标是找出无法吃上三明治的学生数量。两种不同的解法被提出,一种使用队列模拟过程,另一种直接计算想吃每种三明治的学生数量,然后遍历三明治数组确定无人想要的点。
摘要由CSDN通过智能技术生成

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<Integer> 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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值