-
Notifications
You must be signed in to change notification settings - Fork 11
/
QueriesWithFixedLength.java
55 lines (46 loc) Β· 1.67 KB
/
QueriesWithFixedLength.java
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
44
45
46
47
48
49
50
51
52
53
54
55
package queues;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class QueriesWithFixedLength {
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int length = scanner.nextInt();
int queries = scanner.nextInt();
int[] array = getArray(length);
while (queries-- > 0) {
int query = scanner.nextInt();
System.out.println(minimumIn(array, query));
}
}
private static int minimumIn(int[] array, int subLength) {
Queue<Integer> window = new LinkedList<>();
addElementsIn(window, subLength, array);
int maximum = window.stream().max(Integer::compareTo).get();
int minimum = maximum;
for (int index = subLength ; index < array.length ; index++) {
int removed = window.poll();
int inserted = array[index];
if (inserted >= maximum) {
maximum = inserted;
} else if (removed == maximum) {
maximum = window.stream().max(Integer::compareTo).get();
}
window.add(inserted);
minimum = Math.min(minimum, maximum);
}
return minimum;
}
private static void addElementsIn(Queue<Integer> window, int length, int[] array) {
for (int index = 0 ; index < length ; index++) {
window.add(array[index]);
}
}
private static int[] getArray(int length) {
int[] array = new int[length];
for (int index = 0 ; index < array.length ; index++) {
array[index] = scanner.nextInt();
}
return array;
}
}