-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSandwichQueue.java
More file actions
35 lines (28 loc) · 1.14 KB
/
SandwichQueue.java
File metadata and controls
35 lines (28 loc) · 1.14 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
import java.util.*;
public class SandwichQueue {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(); // number of students and sandwiches
Queue<Integer> students = new LinkedList<>();
for (int i = 0; i < n; i++) {
students.offer(sc.nextInt()); // student preferences
}
int[] sandwiches = new int[n];
for (int i = 0; i < n; i++) {
sandwiches[i] = sc.nextInt(); // sandwich stack (top is sandwiches[0])
}
int index = 0;
int count = 0;
while (!students.isEmpty() && count < students.size()) {
if (students.peek() == sandwiches[index]) {
students.poll(); // student gets sandwich
index++; // move to next sandwich
count = 0; // reset unmatched counter
} else {
students.offer(students.poll()); // student moves to back
count++; // increase count of unmatched attempts
}
}
System.out.println(students.size());
}
}