Skip to content

Commit bb7925f

Browse files
solves number of students unable to eat lunch
1 parent 74670a5 commit bb7925f

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@
417417
| 1684 | [Count the Number of Consistent Strings](https://leetcode.com/problems/count-the-number-of-consistent-strings) | [![Java](assets/java.png)](src/CountTheNumberOfConsistentStrings.java) | |
418418
| 1688 | [Count of Matches in Tournament](https://leetcode.com/problems/count-of-matches-in-tournament) | [![Java](assets/java.png)](src/CountOfMatchesInTournament.java) | |
419419
| 1694 | [Reformat Phone Number](https://leetcode.com/problems/reformat-phone-number) | [![Java](assets/java.png)](src/ReformatPhoneNumber.java) | |
420-
| 1700 | [Number of Students Unable to Eat Lunch](https://leetcode.com/problems/number-of-students-unable-to-eat-lunch) | | |
420+
| 1700 | [Number of Students Unable to Eat Lunch](https://leetcode.com/problems/number-of-students-unable-to-eat-lunch) | [![Java](assets/java.png)](src/NumberOfStudentsUnableToEatLunch.java) | |
421421
| 1704 | [Determine if String Halves Are Alike](https://leetcode.com/problems/determine-if-string-halves-are-alike) | | |
422422
| 1708 | 🔒 [Largest Subarray Length K](https://leetcode.com/problems/largest-subarray-length-k/) | | |
423423
| 1710 | [Maximum Units on a Truck](https://leetcode.com/problems/maximum-units-on-a-truck/) | | |

Diff for: src/NumberOfStudentsUnableToEatLunch.java

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import java.util.LinkedList;
2+
import java.util.Queue;
3+
4+
public class NumberOfStudentsUnableToEatLunch {
5+
public int countStudents(int[] students, int[] sandwiches) {
6+
final Queue<Integer> studentQueue = queueFrom(students);
7+
int replacementStudents = 0, stack = 0;
8+
while (!studentQueue.isEmpty() && replacementStudents < studentQueue.size()) {
9+
if (studentQueue.peek() == sandwiches[stack]) {
10+
stack++;
11+
studentQueue.poll();
12+
replacementStudents = 0;
13+
} else {
14+
studentQueue.add(studentQueue.poll());
15+
replacementStudents++;
16+
}
17+
}
18+
return studentQueue.size();
19+
}
20+
21+
private Queue<Integer> queueFrom(int[] array) {
22+
final Queue<Integer> queue = new LinkedList<>();
23+
for (int element : array) {
24+
queue.add(element);
25+
}
26+
return queue;
27+
}
28+
}

0 commit comments

Comments
 (0)