|
| 1 | +<h2><a href="https://leetcode.com/problems/number-of-students-unable-to-eat-lunch">1802. Number of Students Unable to Eat Lunch</a></h2><h3>Easy</h3><hr><p>The school cafeteria offers circular and square sandwiches at lunch break, referred to by numbers <code>0</code> and <code>1</code> respectively. All students stand in a queue. Each student either prefers square or circular sandwiches.</p> |
| 2 | + |
| 3 | +<p>The number of sandwiches in the cafeteria is equal to the number of students. The sandwiches are placed in a <strong>stack</strong>. At each step:</p> |
| 4 | + |
| 5 | +<ul> |
| 6 | + <li>If the student at the front of the queue <strong>prefers</strong> the sandwich on the top of the stack, they will <strong>take it</strong> and leave the queue.</li> |
| 7 | + <li>Otherwise, they will <strong>leave it</strong> and go to the queue's end.</li> |
| 8 | +</ul> |
| 9 | + |
| 10 | +<p>This continues until none of the queue students want to take the top sandwich and are thus unable to eat.</p> |
| 11 | + |
| 12 | +<p>You are given two integer arrays <code>students</code> and <code>sandwiches</code> where <code>sandwiches[i]</code> is the type of the <code>i<sup>th</sup></code> sandwich in the stack (<code>i = 0</code> is the top of the stack) and <code>students[j]</code> is the preference of the <code>j<sup>th</sup></code> student in the initial queue (<code>j = 0</code> is the front of the queue). Return <em>the number of students that are unable to eat.</em></p> |
| 13 | + |
| 14 | +<p> </p> |
| 15 | +<p><strong class="example">Example 1:</strong></p> |
| 16 | + |
| 17 | +<pre> |
| 18 | +<strong>Input:</strong> students = [1,1,0,0], sandwiches = [0,1,0,1] |
| 19 | +<strong>Output:</strong> 0<strong> |
| 20 | +Explanation:</strong> |
| 21 | +- Front student leaves the top sandwich and returns to the end of the line making students = [1,0,0,1]. |
| 22 | +- Front student leaves the top sandwich and returns to the end of the line making students = [0,0,1,1]. |
| 23 | +- Front student takes the top sandwich and leaves the line making students = [0,1,1] and sandwiches = [1,0,1]. |
| 24 | +- Front student leaves the top sandwich and returns to the end of the line making students = [1,1,0]. |
| 25 | +- Front student takes the top sandwich and leaves the line making students = [1,0] and sandwiches = [0,1]. |
| 26 | +- Front student leaves the top sandwich and returns to the end of the line making students = [0,1]. |
| 27 | +- Front student takes the top sandwich and leaves the line making students = [1] and sandwiches = [1]. |
| 28 | +- Front student takes the top sandwich and leaves the line making students = [] and sandwiches = []. |
| 29 | +Hence all students are able to eat. |
| 30 | +</pre> |
| 31 | + |
| 32 | +<p><strong class="example">Example 2:</strong></p> |
| 33 | + |
| 34 | +<pre> |
| 35 | +<strong>Input:</strong> students = [1,1,1,0,0,1], sandwiches = [1,0,0,0,1,1] |
| 36 | +<strong>Output:</strong> 3 |
| 37 | +</pre> |
| 38 | + |
| 39 | +<p> </p> |
| 40 | +<p><strong>Constraints:</strong></p> |
| 41 | + |
| 42 | +<ul> |
| 43 | + <li><code>1 <= students.length, sandwiches.length <= 100</code></li> |
| 44 | + <li><code>students.length == sandwiches.length</code></li> |
| 45 | + <li><code>sandwiches[i]</code> is <code>0</code> or <code>1</code>.</li> |
| 46 | + <li><code>students[i]</code> is <code>0</code> or <code>1</code>.</li> |
| 47 | +</ul> |
0 commit comments