|
| 1 | +/** |
| 2 | + * 1700. Number of Students Unable to Eat Lunch |
| 3 | + * https://leetcode.com/problems/number-of-students-unable-to-eat-lunch/ |
| 4 | + * Difficulty: Easy |
| 5 | + * |
| 6 | + * The school cafeteria offers circular and square sandwiches at lunch break, referred to by |
| 7 | + * numbers 0 and 1 respectively. All students stand in a queue. Each student either prefers |
| 8 | + * square or circular sandwiches. |
| 9 | + * |
| 10 | + * The number of sandwiches in the cafeteria is equal to the number of students. The sandwiches |
| 11 | + * are placed in a stack. At each step: |
| 12 | + * - If the student at the front of the queue prefers the sandwich on the top of the stack, they |
| 13 | + * will take it and leave the queue. |
| 14 | + * - Otherwise, they will leave it and go to the queue's end. |
| 15 | + * |
| 16 | + * This continues until none of the queue students want to take the top sandwich and are thus |
| 17 | + * unable to eat. |
| 18 | + * |
| 19 | + * You are given two integer arrays students and sandwiches where sandwiches[i] is the type of |
| 20 | + * the ith sandwich in the stack (i = 0 is the top of the stack) and students[j] is the |
| 21 | + * preference of the jth student in the initial queue (j = 0 is the front of the queue). |
| 22 | + * Return the number of students that are unable to eat. |
| 23 | + */ |
| 24 | + |
| 25 | +/** |
| 26 | + * @param {number[]} students |
| 27 | + * @param {number[]} sandwiches |
| 28 | + * @return {number} |
| 29 | + */ |
| 30 | +var countStudents = function(students, sandwiches) { |
| 31 | + const preferenceCount = [0, 0]; |
| 32 | + for (const pref of students) { |
| 33 | + preferenceCount[pref]++; |
| 34 | + } |
| 35 | + |
| 36 | + for (const sandwich of sandwiches) { |
| 37 | + if (preferenceCount[sandwich] === 0) { |
| 38 | + return preferenceCount[0] + preferenceCount[1]; |
| 39 | + } |
| 40 | + preferenceCount[sandwich]--; |
| 41 | + } |
| 42 | + |
| 43 | + return 0; |
| 44 | +}; |
0 commit comments