> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Number of Students Unable to Eat Lunch

> Tested Python solution for LeetCode 1700 with 17 pytest cases. Generate a practice environment with lcpy.

LeetCode 1700, [Easy](/catalog/easy). Topics: [Array](/catalog/topics/array), [Stack](/catalog/topics/stack), [Queue](/catalog/topics/queue), [Simulation](/catalog/topics/simulation). [View on LeetCode](https://leetcode.com/problems/number-of-students-unable-to-eat-lunch/description/).

Generate this problem as a practice environment: tested reference solution, 17 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1700   # by problem number
lcpy gen -s number_of_students_unable_to_eat_lunch   # by problem name
```

## Problem

The school cafeteria offers circular and square sandwiches at lunch break, referred to by numbers `0` and `1` respectively. All students stand in a queue. Each student either prefers square or circular sandwiches.

The number of sandwiches in the cafeteria is equal to the number of students. The sandwiches are placed in a **stack**. At each step:

* If the student at the front of the queue **prefers** the sandwich on the top of the stack, they will **take it** and leave the queue.
* Otherwise, they will **leave it** and go to the queue's end.

This continues until none of the queue students want to take the top sandwich and are thus unable to eat.

You are given two integer arrays `students` and `sandwiches` where `sandwiches[i]` is the type of the `i`th sandwich in the stack (`i = 0` is the top of the stack) and `students[j]` is the preference of the `j`th student in the initial queue (`j = 0` is the front of the queue). Return the number of students that are unable to eat.

### Examples

```
Input: students = [1,1,0,0], sandwiches = [0,1,0,1]
Output: 0
```

**Explanation:** Front students who want sandwich `1` move to the end of the queue until the stack exposes `1`, and every student eventually takes a sandwich.

```
Input: students = [1,1,1,0,0,1], sandwiches = [1,0,0,0,1,1]
Output: 3
```

### Constraints

* 1 \<= students.length, sandwiches.length \<= 100
* students.length == sandwiches.length
* sandwiches\[i] is 0 or 1.
* students\[i] is 0 or 1.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/number_of_students_unable_to_eat_lunch/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/number_of_students_unable_to_eat_lunch/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n + m) where n = len(students), m = len(sandwiches)
    # Space: O(1) - only two counters
    def count_students(self, students: list[int], sandwiches: list[int]) -> int:
        counts = [0, 0]
        for pref in students:
            counts[pref] += 1
        for sandwich in sandwiches:
            if counts[sandwich] == 0:
                break
            counts[sandwich] -= 1
        return counts[0] + counts[1]
```

## Complexity

| Time | Space |
| - | - |
| O(n + m) where n = len(students), m = len(sandwiches) | O(1) - only two counters |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.