Repository navigation
Expand file tree
/
Copy pathproblem239.java
More file actions
65 lines (46 loc) · 1.42 KB
/
Copy pathproblem239.java
File metadata and controls
65 lines (46 loc) · 1.42 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
// 239.Sliding Window Maximum
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
class Problem239{
public static int[] maxSlidingWindow(int[] nums, int k) {
List<Integer> an = new ArrayList<>();
int l=0,r=0;
Deque<Integer> dq = new ArrayDeque<>(); //using a deque
while(r<nums.length)
{
while(!dq.isEmpty() && dq.peekLast()<nums[r]) //checking if the top of the queue is lessthan the current (nums[r])
{
dq.removeLast();
}
dq.addLast(nums[r]);
if(r==l+k-1)
{
an.add(dq.peekFirst());
if(dq.peekFirst()==nums[l])
dq.removeFirst();
l++;
}
r++;
if(r==nums.length)
break;
}
int [] ans = new int[an.size()];
for(int i=0;i<an.size();i++) // converting list into array for return type
{
ans[i]=an.get(i);
}
return ans;
}
public static void main(String[] args)
{
int [] n2= {1,3,-1,-3,5,3,6,7};
//int [] n2 = {1};
int [] ans = maxSlidingWindow(n2, 3);
for (int i : ans) {
System.out.print(i);
}
return;
}
}