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.