- Sliding Window Maximum
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.
For example,
Given nums = [1,3,-1,-3,5,3,6,7], and k = 3.
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 3
1 [3 -1 -3] 5 3 6 7 3
1 3 [-1 -3 5] 3 6 7 5
1 3 -1 [-3 5 3] 6 7 5
1 3 -1 -3 [5 3 6] 7 6
1 3 -1 -3 5 [3 6 7] 7
Therefore, return the max sliding window as [3,3,5,5,6,7].
Note:
You may assume k is always valid, ie: 1 ≤ k ≤ input array's size for non-empty array.
Follow up:
Could you solve it in linear time?
my thoughts:
1. min heap.
my solution:
********** 148ms
class Solution(object):
def maxSlidingWindow(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: List[int]
"""
d = collections.deque()
res = []
for i, n in enumerate(nums):
while d and nums[d[-1]] < n:
d.pop()
d.append(i)
if i >= k-1:
res.append(nums[d[0]])
if d[0] == i - k + 1:
d.popleft()
return res
my comments:
from other ppl's solution:
1. I wrote 3 methods I can come up with.
Deque Method: use deque to record the index, remove elements out of window in the head, and remove the smaller elements in the tail which have no change to become max.
// O(n)
public int[] maxSlidingWindow_deque(int[] nums, int k) {
if(nums==null || k>nums.length || k<0) return null;
if(k==0 || nums.length==0) return new int[0];
Deque<Integer> dq = new LinkedList<>();
int[] res = new int[nums.length-k+1];
int index = 0; // index in r is equal to the left side of window
for(int i=0; i<nums.length; i++){
// remove elements out of window
index = i-k+1;
while(!dq.isEmpty() && dq.peek()<index){
dq.poll();
}
// remove smaller elements, which have no change to become max
while(!dq.isEmpty() && nums[dq.peek()]<nums[i]){
dq.pollLast();
}
dq.offer(i);
if(index>=0){
res[index] = nums[dq.peek()];
}
}
return res;
}
Max Heap Method: a naive method. Build a max heap for a window, and remove the element out of window.
//O((n-k)(lgk+k)) = O((n-k)k)
public int[] maxSlidingWindow_heap1(int[] nums, int k) {
if(nums==null || k>nums.length || k<0) return null;
if(k==0 || nums.length==0) return new int[0];
PriorityQueue<Integer> heap = new PriorityQueue<>( k, Collections.reverseOrder() );
int[] res = new int[nums.length - k + 1];
for(int i=0; i<k; i++)
heap.offer(nums[i]);
for(int p=0; p<res.length; p++){
res[p] = heap.peek();
heap.remove(nums[p]);
if(p+k < nums.length)
heap.offer(nums[p+k]);
}
return res;
}
Max Heap Method 2: Using a class Tuple to record index info.
// worst case O((n-k)logn) increasing order, best case O((n-k)logk) decreasing order
public int[] maxSlidingWindow_heap2(int[] nums, int k) {
if(nums==null || k>nums.length || k<0) return null;
if(k==0 || nums.length==0) return new int[0];
PriorityQueue<Tuple> heap = new PriorityQueue<>( k, Collections.reverseOrder() );
int[] res = new int[nums.length - k + 1];
for(int i=0; i<k; i++)
heap.offer(new Tuple(nums[i], i));
Tuple tup = null;
for(int p=0; p<res.length; p++){
tup = heap.peek();
while(tup.idx<p) {
heap.poll();
tup = heap.peek();
}
res[p] = tup.val;
if(p+k < nums.length)
heap.offer(new Tuple(nums[p+k], p+k));
}
return res;
}
class Tuple implements Comparable<Tuple>{
int val;
int idx;
public Tuple(int value, int index){
val = value;
idx = index;
}
@Override
public int compareTo(Tuple tup){
return (val - tup.val);
}
}