Number of Students Unable to Eat Lunch
LeetCode 1700 · எளிது · தீர்வு, intuition & படிப்படியான வாக்த்ரூ
Stop when a full rotation makes no progress.
நான் Number of Students Unable to Eat Lunch (LeetCode 1700) ஐ இப்படித் தீர்த்தேன் — Queue Simulation பேட்டர்னின் ஒரு எளிது பிராப்ளம். கீழே: intuition, படிப்படியான approach, என் accepted Python கோட், animated test cases மற்றும் line-by-line debugger.
கீழேயுள்ள விளக்கம் ஆங்கிலத்தில் உள்ளது.
Intuition
Simulate the line: if the front student wants the top sandwich, both leave; otherwise the student goes to the back. If a whole lap passes with nobody taking a sandwich, the remaining students can never eat.
Approach
- 1
Front matches top sandwich → remove both, reset the counter.
- 2
Otherwise rotate the student to the back and count.
- 3
Counter equals the queue length → stop.
- 4
Return how many students remain.
நான் எப்படித் தீர்த்தேன்
Queue simulationPythonO(n²) time
class Solution:
def countStudents(self, students, sandwiches):
unable = 0
while len(students):
if students[0] == sandwiches[0]:
students.pop(0)
sandwiches.pop(0)
unable = 0
else:
students.append(students.pop(0))
unable += 1
if unable == len(students):
break
return len(students)Number of Students Unable to Eat Lunch — அடிக்கடி கேட்கப்படும் கேள்விகள்
Number of Students Unable to Eat Lunch பின்னாலுள்ள intuition என்ன?
Simulate the line: if the front student wants the top sandwich, both leave; otherwise the student goes to the back. If a whole lap passes with nobody taking a sandwich, the remaining students can never eat.
Number of Students Unable to Eat Lunch (LeetCode 1700) ஐ எப்படித் தீர்ப்பது?
1. Front matches top sandwich → remove both, reset the counter. 2. Otherwise rotate the student to the back and count. 3. Counter equals the queue length → stop. 4. Return how many students remain.
Number of Students Unable to Eat Lunch இன் time மற்றும் space complexity என்ன?
Time: O(n²). Space: O(1). (Accepted approach: Queue simulation.)
Number of Students Unable to Eat Lunch இல் பொதுவான தவறுகள் என்ன?
Counting each preference type gives O(n): stop when the top sandwich type has no takers.