Skip to main content
Back to problems
Leetcode
Medium
Arrays
Queues
Simulation
Number Of Students Unable To Eat Lunch

Simulate students taking sandwiches in order and count how many students cannot eat.

Acceptance 63%
Problem Statement

Number of Students Unable to Eat Lunch

You are given two lists:

  • students: the preferences of students standing in a queue
  • sandwiches: the type of sandwich at each position in the lunch stack

Each student prefers exactly one type of sandwich, represented by 0 or 1. The student at the front of the queue examines the top sandwich.

  • If the student's preference matches the top sandwich, the student takes it and leaves the queue.
  • Otherwise, the student goes to the back of the queue.

This process continues until the student at the front of the queue does not want the top sandwich, and no student in the queue wants that sandwich either. At that point, the remaining students are unable to eat.

Return the number of students who cannot eat lunch.

Input Format

Input

  • students: an integer array of length n, where each value is 0 or 1
  • sandwiches: an integer array of length n, where each value is 0 or 1

The two arrays contain the same number of elements.

Output Format

Output

  • Return an integer: the number of students who are unable to eat lunch.

Constraints

  • 1≤n≤1051 \le n \le 10^5
  • students[i] and sandwiches[i] are each either 0 or 1
  • The lengths of students and sandwiches are equal
Examples
Sample cases returned by the problem API.

Example 1

Input

students = [1,1,0,0]
sandwiches = [0,1,0,1]

Output

0

Explanation

The first student moves to the back, then the top sandwich 0 is taken by a student who wants 0. Eventually all students get a sandwich.

Example 2

Input

students = [1,1,1,0,0,1]
sandwiches = [1,0,0,0,1,1]

Output

3

Explanation

After some students rotate, the remaining students all want 1 but the next sandwich is 0. No one wants it, so 3 students cannot eat.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.