1700.无法吃午餐的学生数量
链接:1700.无法吃午餐的学生数量
难度:Easy
标签:栈、队列、数组、模拟
简介:请你返回无法吃午餐的学生数量。
题解 1 - cpp
- 编辑时间:2022-10-19
- 执行用时:4ms
- 内存消耗:8.4MB
- 编程语言:cpp
- 解法介绍:模拟。
class Solution {
public:
    int countStudents(vector<int>& students, vector<int>& sandwiches) {
        int list[2] = {0};
        for (auto &s : students) list[s]++;
        for (int i = 0; i < sandwiches.size() && list[sandwiches[i]]; i++) list[sandwiches[i]]--;
        return list[0] + list[1];
    }
};
题解 2 - cpp
- 编辑时间:2022-10-19
- 内存消耗:8MB
- 编程语言:cpp
- 解法介绍:模拟。
class Solution {
public:
    int countStudents(vector<int>& students, vector<int>& sandwiches) {
        int n = sandwiches.size(), list[2] = {0};
        queue<int> q1, q2;
        for (auto &s : students) q1.push(s), list[s]++;;
        for (auto &s : sandwiches) q2.push(s);
        while (q2.size()) {
            int size = q2.size();
            while (q1.size() && q1.front() == q2.front()) list[q1.front()]--, q1.pop(), q2.pop();
            if (q2.size()) q1.push(q1.front()), q1.pop();
            if (q2.size() && list[q2.front()] == 0) break;
        }
        return q1.size();
    }
};