Skip to content

Commit 778ad52

Browse files
authored
Create WindowMax.java
1 parent acc725a commit 778ad52

1 file changed

Lines changed: 83 additions & 0 deletions

File tree

Window/WindowMax.java

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import java.util.*;
2+
public class WindowMax{
3+
4+
private LinkedList<Integer> list;
5+
private int L;
6+
private int R;
7+
private int winMax;
8+
private int[] array;
9+
10+
public WindowMax(int[] arr,int _winMax){
11+
list=new LinkedList<Integer> ();
12+
L=0;
13+
R=0;
14+
winMax = _winMax;
15+
array = arr;
16+
17+
for (int i=0; i<_winMax && i<arr.length; i++) {
18+
toRight();
19+
}
20+
21+
}
22+
23+
public int getMax(){
24+
if(!list.isEmpty()){
25+
return array[list.peekFirst()];
26+
}
27+
return Integer.MIN_VALUE;
28+
}
29+
30+
public WindowMax toLeft(){
31+
while(!list.isEmpty()){
32+
int first = list.peekFirst();
33+
if(first > L){
34+
break;
35+
}
36+
list.removeFirst();
37+
}
38+
L++;
39+
return this;
40+
}
41+
42+
public WindowMax toRight(){
43+
if(list.isEmpty()){
44+
list.offerLast(R);
45+
}
46+
else{
47+
while(!list.isEmpty()){
48+
if(array[list.peekLast()] > array[R]){
49+
break;
50+
}
51+
list.removeLast();
52+
}
53+
list.offerLast(R);
54+
}
55+
R++;
56+
return this;
57+
}
58+
59+
60+
public static void main(String[] args){
61+
62+
List<Integer> result = new ArrayList<>();
63+
int[] arr=new int[]{4,3,5,4,3,3,6,7,8,5,9};
64+
int win_max=24;
65+
66+
WindowMax win=new WindowMax(arr,win_max);
67+
result.add(win.getMax());
68+
69+
for (int i=win_max; i<arr.length; i++) {
70+
win.toLeft().toRight();
71+
result.add(win.getMax());
72+
}
73+
74+
for (int v:result ) {
75+
System.out.print(v+",");
76+
}
77+
System.out.println();
78+
}
79+
80+
}
81+
82+
83+

0 commit comments

Comments
 (0)