-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathSlidingWindowMaximum.java
More file actions
51 lines (45 loc) · 1.49 KB
/
SlidingWindowMaximum.java
File metadata and controls
51 lines (45 loc) · 1.49 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
// Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position. Return the max sliding window.
import java.util.Deque;
import java.util.LinkedList;
public class SlidingWindowMaximum
{
public int[] slidingWindowMaximum(int a[], int k)
{
int n = a.length;
Deque<Integer> dq = new LinkedList<Integer>();
int ans[] = new int[n-k+1];
int i = 0;
for(; i<k; i++)
{
while(!dq.isEmpty() && a[dq.peekLast()] <=a[i])
{
dq.removeLast();
}
dq.addLast(i);
}
for(; i<n; i++)
{
ans[i-k] = a[dq.peekFirst()];
while(!dq.isEmpty() && dq.peekFirst() <= (i-k))
{
dq.removeFirst();
}
while(!dq.isEmpty() && a[dq.peekLast()] <= a[i])
{
dq.removeLast();
}
dq.addLast(i);
}
ans[i-k] = a[dq.peekFirst()];
return ans;
}
public static void main(String args[])
{
int a[] = {1, 3, -1, -3, 5, 3, 6, 7};
SlidingWindowMaximum obj = new SlidingWindowMaximum();
int answer[] = obj.slidingWindowMaximum(a, 3);
for (int i = 0; i < answer.length; i++) {
System.out.println(answer[i]);
}
}
}