摘要:丟棄隊首那些超出窗口長度的元素隊首的元素都是比后來加入元素大的元素,所以存儲的對應的元素是從小到大
Problem
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?
class Solution { public int[] maxSlidingWindow(int[] nums, int k) { PriorityQueueusing Deque to maintain a window O(n)queue = new PriorityQueue<>((i1, i2)->i2-i1); if (nums == null || k == 0 || nums.length < k) return new int[0]; int[] res = new int[nums.length-k+1]; for (int i = 0; i < k; i++) queue.offer(nums[i]); res[0] = queue.peek(); for (int i = k; i < nums.length; i++) { queue.remove(nums[i-k]); queue.offer(nums[i]); res[i-k+1] = queue.peek(); } return res; } }
class Solution { public int[] maxSlidingWindow(int[] nums, int k) { if (nums == null || k == 0 || nums.length < k) return new int[0]; int[] res = new int[nums.length-k+1]; int index = 0; Dequequeue = new ArrayDeque<>(); //queue to save index for (int i = 0; i < nums.length; i++) { //丟棄隊首那些超出窗口長度的元素 index < i-k+1 if (!queue.isEmpty() && queue.peek() < i-k+1) queue.poll(); //隊首的元素都是比后來加入元素大的元素,所以存儲的index對應的元素是從小到大 while (!queue.isEmpty() && nums[queue.peekLast()] < nums[i]) queue.pollLast(); queue.offer(i); if (i >= k-1) res[index++] = nums[queue.peek()]; } return res; } }
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/72277.html
摘要:題目要求假設有一個數組和一個長度為的窗口,數組長度。當窗口右滑時,會刪除下標上的值,并加入下標上的值。此時中記錄的值編程了,并返回當前的最大值為。一旦最大值失效,就從窗口中重新找一個最大值就好了。 題目要求 Given an array nums, there is a sliding window of size k which is moving from the very lef...
摘要:你只可以看到在滑動窗口內的數字?;瑒哟翱诿看沃幌蛴乙苿右晃弧7祷鼗瑒哟翱谧畲笾?。算法思路暴力破解法用兩個指針,分別指向窗口的起始位置和終止位置,然后遍歷窗口中的數據,求出最大值向前移動兩個指針,然后操作,直到遍歷數據完成位置。 Time:2019/4/16Title: Sliding Window MaximumDifficulty: DifficultyAuthor: 小鹿 題目...
摘要:窗口前進,刪隊首元素保證隊列降序加入當前元素下標從開始,每一次循環都將隊首元素加入結果數組 Sliding Window Maximum Problem Given an array of n integer with duplicate number, and a moving window(size k), move the window at each iteration fro...
摘要:這樣,我們可以保證隊列里的元素是從頭到尾降序的,由于隊列里只有窗口內的數,所以他們其實就是窗口內第一大,第二大,第三大的數。 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...
摘要:題目鏈接這道題用,注意一下存的是,因為要判斷是否到最大的值,是通過現在的和第一個的差來判斷的。 Sliding Window Maximum 題目鏈接:https://leetcode.com/problems... 這道題用deque,注意一下存的是index,因為要判斷是否到最大的window值,是通過現在的index和deque第一個index的差來判斷的。 public cla...
閱讀 766·2023-04-25 17:33
閱讀 3626·2021-07-29 14:49
閱讀 2480·2019-08-30 15:53
閱讀 3435·2019-08-29 16:27
閱讀 2000·2019-08-29 16:11
閱讀 1030·2019-08-29 14:17
閱讀 2432·2019-08-29 13:47
閱讀 2016·2019-08-29 13:28