|
| 1 | +# 1700. Number of Students Unable to Eat Lunch |
| 2 | + |
| 3 | +- Difficulty: Easy. |
| 4 | +- Related Topics: Array, Stack, Queue, Simulation. |
| 5 | +- Similar Questions: Time Needed to Buy Tickets. |
| 6 | + |
| 7 | +## Problem |
| 8 | + |
| 9 | +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. |
| 10 | + |
| 11 | +The number of sandwiches in the cafeteria is equal to the number of students. The sandwiches are placed in a **stack**. At each step: |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +- 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. |
| 16 | + |
| 17 | +- Otherwise, they will **leave it** and go to the queue's end. |
| 18 | + |
| 19 | + |
| 20 | +This continues until none of the queue students want to take the top sandwich and are thus unable to eat. |
| 21 | + |
| 22 | +You are given two integer arrays `students` and `sandwiches` where `sandwiches[i]` is the type of the `ith` sandwich in the stack (`i = 0` is the top of the stack) and `students[j]` is the preference of the `jth` student in the initial queue (`j = 0` is the front of the queue). Return **the number of students that are unable to eat.** |
| 23 | + |
| 24 | + |
| 25 | +Example 1: |
| 26 | + |
| 27 | +``` |
| 28 | +Input: students = [1,1,0,0], sandwiches = [0,1,0,1] |
| 29 | +Output: 0 |
| 30 | +Explanation: |
| 31 | +- Front student leaves the top sandwich and returns to the end of the line making students = [1,0,0,1]. |
| 32 | +- Front student leaves the top sandwich and returns to the end of the line making students = [0,0,1,1]. |
| 33 | +- Front student takes the top sandwich and leaves the line making students = [0,1,1] and sandwiches = [1,0,1]. |
| 34 | +- Front student leaves the top sandwich and returns to the end of the line making students = [1,1,0]. |
| 35 | +- Front student takes the top sandwich and leaves the line making students = [1,0] and sandwiches = [0,1]. |
| 36 | +- Front student leaves the top sandwich and returns to the end of the line making students = [0,1]. |
| 37 | +- Front student takes the top sandwich and leaves the line making students = [1] and sandwiches = [1]. |
| 38 | +- Front student takes the top sandwich and leaves the line making students = [] and sandwiches = []. |
| 39 | +Hence all students are able to eat. |
| 40 | +``` |
| 41 | + |
| 42 | +Example 2: |
| 43 | + |
| 44 | +``` |
| 45 | +Input: students = [1,1,1,0,0,1], sandwiches = [1,0,0,0,1,1] |
| 46 | +Output: 3 |
| 47 | +``` |
| 48 | + |
| 49 | + |
| 50 | +**Constraints:** |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | +- `1 <= students.length, sandwiches.length <= 100` |
| 55 | + |
| 56 | +- `students.length == sandwiches.length` |
| 57 | + |
| 58 | +- `sandwiches[i]` is `0` or `1`. |
| 59 | + |
| 60 | +- `students[i]` is `0` or `1`. |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | +## Solution |
| 65 | + |
| 66 | +```javascript |
| 67 | +/** |
| 68 | + * @param {number[]} students |
| 69 | + * @param {number[]} sandwiches |
| 70 | + * @return {number} |
| 71 | + */ |
| 72 | +var countStudents = function(students, sandwiches) { |
| 73 | + var numOfOnes = 0; |
| 74 | + var numOfZeros = 0; |
| 75 | + for (var i = 0; i < students.length; i++) { |
| 76 | + if (students[i] === 1) numOfOnes++; |
| 77 | + else numOfZeros++; |
| 78 | + } |
| 79 | + for (var j = 0; j < sandwiches.length; j++) { |
| 80 | + if (sandwiches[j] === 1) { |
| 81 | + if (numOfOnes > 0) numOfOnes--; |
| 82 | + else break; |
| 83 | + } else { |
| 84 | + if (numOfZeros > 0) numOfZeros--; |
| 85 | + else break; |
| 86 | + } |
| 87 | + } |
| 88 | + return numOfOnes + numOfZeros; |
| 89 | +}; |
| 90 | +``` |
| 91 | + |
| 92 | +**Explain:** |
| 93 | + |
| 94 | +nope. |
| 95 | + |
| 96 | +**Complexity:** |
| 97 | + |
| 98 | +* Time complexity : O(n). |
| 99 | +* Space complexity : O(1). |
0 commit comments