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.