【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();
    }
};

相关推荐

  1. leetcode

    2024-04-09 22:44:02       57 阅读
  2. leetcode

    2024-04-09 22:44:02       57 阅读
  3. leetcode

    2024-04-09 22:44:02       65 阅读
  4. LeetCode

    2024-04-09 22:44:02       36 阅读
  5. leetcode

    2024-04-09 22:44:02       32 阅读
  6. Leetcode -2

    2024-04-09 22:44:02       52 阅读
  7. Leetcode】计算器

    2024-04-09 22:44:02       65 阅读
  8. LeetCode 45

    2024-04-09 22:44:02       69 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-09 22:44:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-09 22:44:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-09 22:44:02       82 阅读
  4. Python语言-面向对象

    2024-04-09 22:44:02       91 阅读

热门阅读

  1. MSsql数据库 sql注入

    2024-04-09 22:44:02       36 阅读
  2. 进程替换exec系列介绍

    2024-04-09 22:44:02       37 阅读
  3. Linux C++ 022-函数模板

    2024-04-09 22:44:02       34 阅读
  4. python把视频按帧转化为图片并保存

    2024-04-09 22:44:02       35 阅读
  5. Linux下I2C驱动框架:I2C 设备驱动

    2024-04-09 22:44:02       42 阅读
  6. 【Linux篇】makefile一次形成两个可执行

    2024-04-09 22:44:02       34 阅读
  7. Kubernetes统一管理vGPU:原理、实现与挑战

    2024-04-09 22:44:02       40 阅读
  8. 大模型从入门到应用——OpenAI基础调用

    2024-04-09 22:44:02       39 阅读
  9. 程序员如何搞副业?

    2024-04-09 22:44:02       38 阅读
  10. Go语言支持重载吗?如何实现重写?

    2024-04-09 22:44:02       41 阅读
  11. @Transactional 详解

    2024-04-09 22:44:02       35 阅读
  12. 雷军给年轻人的五点建议

    2024-04-09 22:44:02       28 阅读