-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueuePractice.java
More file actions
43 lines (38 loc) · 1.02 KB
/
QueuePractice.java
File metadata and controls
43 lines (38 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package Stack;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
/*
1700 - Number of students unable to eat
*/
public class QueuePractice {
public static int countStudents(int[] students, int[] sandwiches) {
int count =0;
Queue<Integer> q = new LinkedList<>();
Stack<Integer> stack = new Stack<>();
for(int student: students){
q.add(student);
}
for(int i = sandwiches.length-1; i>=0;i--){
stack.add(sandwiches[i]);
}
int sand = stack.pop();
while(!q.isEmpty() && count<students.length){
int student = q.poll();
if(sand==student){
if(!stack.isEmpty()){
sand = stack.pop();
}
count=0;
}
else {
q.add(student);
count++;
}
if(count>students.length){
q.add(student);
}
}
return q.size();
}
}