题解 | 无法吃午餐的学生数量
无法吃午餐的学生数量
https://www.nowcoder.com/practice/2dac3d7567f741a88ec551caf907934d
#include <queue>
#include <vector>
using namespace std;
class Solution {
public:
int countStudents(vector<int>& students, vector<int>& sandwiches) {
queue<int> q;
for(int s:students) q.push(s);
int idx=0;
int i=0;
// 关键点,一个队列,一个栈
// 将students转为队列
// 根据idx的递增,将动态数组sandwichs模拟为栈,此题对栈的操作只有出栈
// 当i>q.size()时,就代表遍历完成,此时全为没吃午餐的人
while(i<q.size()){
if(q.front()==sandwiches[idx]){
q.pop();
idx++;
i=0;
}else{
q.push(q.front());
q.pop();
i+=1;
}
}
return q.size();
}
};
