239. Sliding Window Maximum/ min
239. Sliding Window Maximum/ min https://www.youtube.com/watch?v=2SXqBsTR6a8 https://www.youtube.com/watch?v=G70qltIBF40 过例子, 写自己的 思路, 然后写自己的代码 class Solution { public int[] maxSlidingWindow(int[] nums, int k) { Deque<Integer> deque = new LinkedList<>(); List<Integer> result = new ArrayList<>(); for(int i = 0; i < nums.length; i++){ while(!deque.isEmpty() && nums[i] >= nums[deque.peekLast()]){ deque.pollLast(); // if deque is not empty and the new coming elements is bigger than the element at the end of the deque, then we pop them from the end. the reason that we pop elements from the back is because, the deque is in decreasing order, which means, example, in this example, i use the value , instead of index. but its the same since we refer to nums[index]. : [6, 4,] if the new coming element is 5, then we need to pop 4 out of the deque and still keep 6 . if we pop them from the front, there is no way we can pop 4 from the deque. } deque.offerLast(i); // offer the new element, no matter if its smaller or bigger, because it has the potential to be the max later if(i - k + 1 >= 0){ // after index k - 1, its guarentted that we have a window of size k, so we output a max value to the result everytime after index k - 1, which is i - k + 1 >= 0. result.add(nums[deque.peekFirst()]); // since the deque is in decreasing order, and the max value is at the front of the deque } if(i - k + 1 == deque.peekFirst()){ // this is to remove the front element from the deque, if the front element happens to be the element where the slding window is leaving behind (this happens when the front element is the largest but since we are moving the sliding window so we have to remove this element from the sliding window and the deque as well) deque.pollFirst(); } } int[] res = new int[result.size()]; for(int m = 0; m < result.size(); m++){ // i can't be used again here, defined above already res[m] = result.get(m); } return res; } } Min from a fixed sliding window 的变种成了求min
use maxheap Not a linear solution, instead, it is of O(nlogk) complexity, since add, pop and remove operation of PriorityQueue cost O(logk) time. What we need to do is just maintain a heap, that heap top gets the maximal value of the k elements. Since we know which element is removed and which is added to the queue, the solution seems easy to understand. public class Solution { public int[] maxSlidingWindow(int[] nums, int k) { int len = nums.length; int[] result = new int[len - k + 1]; if(nums.length == 0) return new int[0]; Queue<Integer> queue = new PriorityQueue<Integer>(new Comparator<Integer>(){ @Override public int compare(Integer i1, Integer i2){ return Integer.compare(i2, i1); } }); for(int i = 0; i < k; i ++){ queue.add(nums[i]); } result[0] = queue.peek(); for(int i = k; i < len; i ++){ queue.remove(nums[i - k]); queue.add(nums[i]); result[i - k + 1] = queue.peek(); } return result; } Could somebody suggest some linear solutions? The hint of using deque seems not that reasonable. We still need to maintain the k elements in the window in order.
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.
Example:
Input: nums =[1,3,-1,-3,5,3,6,7], and k = 3 Output:[3,3,5,5,6,7] Explanation: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
Note:
You may assume k is always valid, 1 ≤ k ≤ input array's size for non-empty array.
Follow up:
Could you solve it in linear time?
posted on 2018-08-09 18:16 猪猪🐷 阅读(156) 评论(0) 收藏 举报
浙公网安备 33010602011771号