【Leetcode】【240408】1700. Number of Students Unable to Eat Lunch

端不出来本周组会的屎了……尽管不止一位朋友/前辈说过:想做SDE工作的话,科研能划就划,重在练习日语。。。
BGM:江南-林俊杰《2003-2010精选》

Descripition

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

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.

Solution

Caution: The order of initiaization order of stack sandwiches

Code

class Solution {
public:
    int countStudents(vector<int>& students, vector<int>& sandwiches)
    {
        queue<int>order_stu;
        stack<int>order_sand;
        for(int i=0;i<students.size();++i)
        {
            order_stu.push(students[i]);
        }
        for(int i=sandwiches.size()-1;i>=0;--i)
        {
            order_sand.push(sandwiches[i]);
        }
        int precnt=0,cnt=1;
        while(!order_stu.empty())
        {
            if(order_stu.front()==order_sand.top())
            {
                order_stu.pop();
                order_sand.pop();
            }
            else
            {
                int mid=order_stu.front();
                //cout<<mid<<endl;
                order_stu.pop();
                order_stu.push(mid);
            }
            if(cnt<order_stu.size())
            {
                ++cnt;
            }
            else
            {
                if(precnt==cnt) break;
                else
                {
                    precnt=cnt;
                    cnt=0;
                }
            }
        }
        return order_stu.size();
    }
};
  • 6
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值