Number of Students Unable to Eat Lunch LeetCode solution in Java

1
class Solution {
2
public int countStudents(int[] students, int[] sandwiches) {
3
int ones = 0; // count of students who prefer type1
4
int zeros = 0; // count of students who prefer type0
5

6
for (int stud : students) {
7
if (stud == 0) zeros++;
8
else ones++;
9
}
10

11
// for each sandwich in sandwiches
12
for (int sandwich : sandwiches) {
13
if (sandwich == 0) { // if sandwich is of type0
14
if (zeros == 0) { // if no student want a type0 sandwich
15
return ones;
16
}
17
zeros--;
18
} else { // if sandwich is of type1
19
if (ones == 0) { // if no student want a type1 sandwich
20
return zeros;
21
}
22
ones--;
23
}
24
}
25
return 0;
26
}
27
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0