NeetCode #85LC-1700EasyArrays & Hashing
← Back to All Problems#85 · #1700 · Number of Students Unable to Eat Lunch(无法吃午餐的学生数量)
📌 Problem Statement & Constraints
Students queue for lunch.
students[i] is the sandwich preference of the i-th student (0 or 1) and sandwiches[j] is the type of the j-th sandwich in the stack (top first). Each student at the front either takes the top sandwich if it matches, or goes to the back. Return the number of students unable to eat. Constraints: 1 <= students.length == sandwiches.length <= 100.💡 Core Algorithmic Approaches
- Simulating the queue literally is O(n^2) in the worst case, because a student may cycle through the queue many times.
- The key insight: if the top sandwich matches nobody in the queue, nobody will ever eat again, and the process halts.
- So count how many students prefer each type, then consume sandwiches from the top: decrement the matching count, and stop as soon as the top sandwich's type has zero remaining students.
- The answer is the number of students still left, which is the sum of the remaining counts.
💻 Benchmark Python3 Implementation
class Solution:
def countStudents(self, students: List[int], sandwiches: List[int]) -> int:
from collections import Counter
cnt = Counter(students)
for s in sandwiches: # top of the stack first
if cnt[s] == 0: # no student wants this sandwich -> stuck
break
cnt[s] -= 1
return sum(cnt.values())⚡ Complexity Deep Dive
⏱️ Time Complexity
O(n): one pass to count and one pass over the sandwiches, with no queue simulation.
💾 Space Complexity
O(1): the counter has at most two entries.
⚠️ Interview Pitfalls & Follow-ups
- Simulating the queue with a deque: correct but O(n^2) worst case; the counting insight makes it linear.
- Continuing past an unconsumable sandwich: once the top sandwich has no taker, the process is permanently stuck, so break immediately.
- Counting the sandwiches instead of the students: you need to know which preferences remain.
- Returning the number of consumed sandwiches: the answer is the number of students unable to eat.
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
🧩 Related Arrays & Hashing Problems
中文速览与面试要点
无法吃午餐的学生数量
学校的自助午餐提供圆形和方形三明治,分别用数字 0 和 1 表示。所有学生站在一个队列里,每个学生要么喜欢圆形三明治、要么喜欢方形三明治。三明治的数量与学生数量相同。每一轮:如果队列最前面的学生喜欢栈顶的三明治,那么他会拿走它并离开队列;否则他会放弃这个三明治,回到队列的尾部。这个过程会一直持续到队列里没有学生喜欢栈顶的三明治为止。请你返回无法吃午餐的学生数量。约束:1 ≤ students.length == sandwiches.length ≤ 100;students[i] 与 sandwiches[i] 为 0 或 1。
核心要点
- 关键洞察:顺序不重要(标准解) :学生可以无限次回到队尾,因此队列的 顺序 对最终结果没有影响,只取决于 各类型学生的数量 。
- 模拟三明治栈 :按 sandwiches 的顺序依次取三明治,若当前类型的需求人数 > 0 则消耗一个;若需求为 0,说明剩下的学生都不喜欢这个三明治,且他们会一直循环,因此无法继续,直接返回剩余学生数。
- 返回剩余人数 : len(sandwiches) - i ,其中 i 是当前三明治的下标。
- 为什么不需要真的模拟队列 :队列的循环特性抹掉了顺序信息,只保留计数信息——这是本题的核心简化。
常见陷阱
- 真的用双端队列模拟 :虽然可行,但会退化为 O(n²)(每个学生可能回到队尾多次),且实现复杂。顺序无关的观察是本题的核心。
- 卡住时返回 len(students) :必须返回 剩余 学生数,即 len(sandwiches) - i (因为三明治与学生总数相同,等价于剩余学生数)。
- 误以为需要判断「所有学生都不喜欢」 :只要 当前 栈顶三明治无人喜欢就会永久卡住,无需考虑后续三明治。
参考实现
from collections import Counter
class Solution:
def countStudents(self, students: list[int], sandwiches: list[int]) -> int:
cnt = Counter(students) # 各类学生的需求数量
for i, s in enumerate(sandwiches):
if cnt[s] == 0: # 没有学生喜欢这个三明治 -> 卡住
return len(sandwiches) - i
cnt[s] -= 1
return 0