File: number-of-students-unable-to-eat-lunch/solution.py

Date: 2026-06-06

Time: 18:23

Purpose

This file solves LeetCode 1700: Number of Students Unable to Eat Lunch. The problem simulates a cafeteria queue: students line up for sandwiches, each student prefers either circular (0) or square (1) sandwiches, and the sandwich stack is served top-first. A student who doesn't like the top sandwich goes to the back of the line. The process deadlocks when no remaining student wants the top sandwich.

Key Components

countStudents(students, sandwiches) -> int

The main solver. Takes two lists of 0s and 1s and returns how many students can't eat.

Contract: students contains preferences (0 or 1), sandwiches is an ordered stack (index 0 = top). Returns a non-negative integer in [0, len(students)].

mintimetoremoveballoons

A module-level alias pointing to countStudents. Likely exists because the test harness or a related problem variant expects this name.

Patterns

Counter-based simulation avoidance. The naive approach simulates the queue rotation — O(n^2) in the worst case. This solution recognizes that the queue order doesn't matter: what matters is *whether any student remaining wants the current top sandwich*. A Counter tracks how many students want each type. This reduces the problem to a single linear scan of the sandwich stack.

This is a common idiom in this repo's solutions: replace simulation with counting/frequency analysis when the order of consumption is determined by supply, not queue position.

Dependencies

Imports: collections.Counter — the only dependency. No custom modules.

Imported by: The test_solution.py in this same directory. The massive "Imported By" list in the metadata is an artifact of the repo's test infrastructure — those test files likely share a common import pattern, not a direct dependency on this module.

Flow

1. Count how many students want type-0 vs type-1 sandwiches.

2. Iterate through sandwiches top-to-bottom.

3. For each sandwich s: if count[s] == 0, no remaining student wants it — the queue is deadlocked. Return the sum of all remaining students (count[0] + count[1]).

4. Otherwise, decrement count[s] (one student of that preference eats) and continue.

5. If all sandwiches are consumed, return 0.

Invariants

Error Handling

None. The function assumes valid input per LeetCode constraints (non-empty lists of 0s and 1s, equal lengths). No bounds checking, type validation, or exception handling — appropriate for a competitive programming solution.

Topics to Explore

Beliefs