-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSlidingWindowMaximum.java
More file actions
128 lines (101 loc) · 3.67 KB
/
SlidingWindowMaximum.java
File metadata and controls
128 lines (101 loc) · 3.67 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package array;
import java.util.*;
/**
* Description: https://leetcode.com/problems/sliding-window-maximum
* Difficulty: Hard
*/
public class SlidingWindowMaximum {
/**
* Time complexity: O(n)
* Space complexity: O(k)
*/
public int[] maxSlidingWindowViaMonotonicDequeWithSingleLoop(int[] nums, int k) {
List<Integer> result = new ArrayList<>();
Deque<Integer> window = new LinkedList<>(); // head always contains an index of the max value in the window
for (int current = 0; current < nums.length; current++) {
// remove leftmost index, that is out of window range
if (!window.isEmpty() && window.peekFirst() == current - k) {
window.pollFirst();
}
// remove indices, whose corresponding numbers are less or equal than current value
while (!window.isEmpty() && nums[window.peekLast()] <= nums[current]) {
window.pollLast();
}
// add index of the current value
window.offerLast(current);
if (current >= k - 1) {
result.add(nums[window.peekFirst()]);
}
}
return result.stream().mapToInt(v -> v).toArray();
}
/**
* Time complexity: O(n)
* Space complexity: O(k)
*/
public int[] maxSlidingWindowViaMonotonicDequeWithTwoLoops(int[] nums, int k) {
List<Integer> result = new ArrayList<>();
Deque<Integer> window = new LinkedList<>();
int left = 0;
int right = 0;
while (right < k) {
while (!window.isEmpty() && nums[window.peekLast()] <= nums[right]) {
window.pollLast();
}
window.offerLast(right);
right++;
}
result.add(nums[window.peekFirst()]);
while (right < nums.length) {
if (!window.isEmpty() && window.peekFirst() == left) {
window.pollFirst();
}
while (!window.isEmpty() && nums[window.peekLast()] <= nums[right]) {
window.pollLast();
}
window.offerLast(right);
result.add(nums[window.peekFirst()]);
left++;
right++;
}
return result.stream().mapToInt(v -> v).toArray();
}
/**
* Time complexity: O(n * (k + log k))
* Space complexity: O(k)
*/
public int[] maxSlidingWindowViaMaxHeap(int[] nums, int k) {
List<Integer> result = new ArrayList<>();
Queue<Integer> windowMaxHeap = new PriorityQueue<>((a, b) -> Integer.compare(b, a));
int left = 0;
int right = 0;
while (right < k) {
windowMaxHeap.offer(nums[right]);
right++;
}
result.add(windowMaxHeap.peek());
while (right < nums.length) {
windowMaxHeap.remove(nums[left]); // iterates through every element and ruins the performance
windowMaxHeap.offer(nums[right]);
result.add(windowMaxHeap.peek());
left++;
right++;
}
return result.stream().mapToInt(v -> v).toArray();
}
/**
* Time complexity: O(n * k)
* Space complexity: O(k)
*/
public int[] maxSlidingWindowNaiveApproach(int[] nums, int k) {
List<Integer> result = new ArrayList<>();
for (int windowStart = 0; windowStart < nums.length - k + 1; windowStart++) {
int localMax = Integer.MIN_VALUE;
for (int shift = 0; shift < k; shift++) {
localMax = Math.max(localMax, nums[windowStart + shift]);
}
result.add(localMax);
}
return result.stream().mapToInt(v -> v).toArray();
}
}