三亩地 三亩地SAN MU DI · CODE DIARY
ARTICLE DETAIL

日记详情

真实记录编程学习的某一天,欢迎挑你感兴趣的翻一翻。

leetcode 耗时100 1700. Number of Students Unable to Eat Lunch

leetcode 耗时100 1700. Number of Students Unable to Eat Lunch

Problem: 1700. 无法吃午餐的学生数量

双端队列的模拟学生,单指针向后移动,若连续队首学生和sandwich都不相同的统计数量num > std.size()就返回

Code

class Solution { public: int countStudents(vector<int>& students, vector<int>& sandwiches) { deque<int> stu; int n = students.size(); for( int i = 0; i < n; i++ ) { stu.push_back(students[i]); } int i = 0, num = 0; while( i < n ) { if(stu.front() == sandwiches[i]) { i++; stu.pop_front(); num = 0; } else { stu.push_back(stu.front()); stu.pop_front(); num++; } if(num > stu.size()) break; } return stu.size(); } };
← 返回列表