-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindowMax.java
More file actions
83 lines (66 loc) · 1.29 KB
/
WindowMax.java
File metadata and controls
83 lines (66 loc) · 1.29 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import java.util.*;
public class WindowMax{
private LinkedList<Integer> list;
private int L;
private int R;
private int winMax;
private int[] array;
public WindowMax(int[] arr,int _winMax){
list=new LinkedList<Integer> ();
L=0;
R=0;
winMax = _winMax;
array = arr;
for (int i=0; i<_winMax && i<arr.length; i++) {
toRight();
}
}
public int getMax(){
if(!list.isEmpty()){
return array[list.peekFirst()];
}
return Integer.MIN_VALUE;
}
public WindowMax toLeft(){
while(!list.isEmpty()){
int first = list.peekFirst();
if(first > L){
break;
}
list.removeFirst();
}
L++;
return this;
}
public WindowMax toRight(){
if(list.isEmpty()){
list.offerLast(R);
}
else{
while(!list.isEmpty()){
if(array[list.peekLast()] > array[R]){
break;
}
list.removeLast();
}
list.offerLast(R);
}
R++;
return this;
}
public static void main(String[] args){
List<Integer> result = new ArrayList<>();
int[] arr=new int[]{4,3,5,4,3,3,6,7,8,5,9};
int win_max=24;
WindowMax win=new WindowMax(arr,win_max);
result.add(win.getMax());
for (int i=win_max; i<arr.length; i++) {
win.toLeft().toRight();
result.add(win.getMax());
}
for (int v:result ) {
System.out.print(v+",");
}
System.out.println();
}
}