-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmaxFromWindowOfN.java
More file actions
53 lines (39 loc) · 949 Bytes
/
maxFromWindowOfN.java
File metadata and controls
53 lines (39 loc) · 949 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package lecture9a14;
import java.util.Scanner;
import lecture9a13.queue;
public class maxFromWindowOfN {
static Scanner a = new Scanner(System.in);
public static void main(String[] args) throws Exception {
int n = a.nextInt();
int[] arr = new int[n];
int i = 0;
while (i < n) {
arr[i] = a.nextInt();
i++;
}
int k = a.nextInt();
max(arr, k);
}
public static void max(int[] arr, int k) throws Exception {
queue q = new queue(1);
int max = Integer.MIN_VALUE;
for (int i = 0; i < k; i++)
if (arr[i] > max) {
max=arr[i];
try{q.dequeue();}catch(Exception e) {
}}
q.enqueue(i);
}
for (int i = k; i < arr.length; i++) {
System.out.println(arr[q.getFront()]);
// remove
if (!q.isEmpty() && q.getFront() <= i - k)
q.dequeue();
// insert
if (arr[i] > max) {
max=arr[i];
q.enqueue(i);}
}
System.out.println(arr[q.getFront()]);
}
}