-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprblm1700.java
More file actions
36 lines (31 loc) · 952 Bytes
/
prblm1700.java
File metadata and controls
36 lines (31 loc) · 952 Bytes
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
import java.util.*;
public class prblm1700 {
public static void main(String[] args) {
int[] students = {1,1,0,0};
int[] sandwiches = {0,1,0,1};
System.out.println(countStudents(students, sandwiches));
}
public static int countStudents(int[] students, int[] sandwiches) {
ArrayList<Integer> list = new ArrayList<>();
for (int student : students) {
list.add(student);
}
Stack<Integer> stack = new Stack<>();
for(int i = sandwiches.length-1; i >= 0; i--){
stack.push(sandwiches[i]);
}
for(int i = 0; i < list.size(); i++){
if(students[i] == stack.peek()){
stack.pop();
}
else{
int removed = list.remove(i);
list.add(removed);
}
}
if (stack.isEmpty()) {
return 0;
}
return stack.size();
}
}