国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

[LeetCode] 239. Sliding Window Maximum

lentoo / 461人閱讀

摘要:丟棄隊(duì)首那些超出窗口長(zhǎng)度的元素隊(duì)首的元素都是比后來加入元素大的元素,所以存儲(chǔ)的對(duì)應(yīng)的元素是從小到大

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?

Solution using max heap O(nlogn)
class Solution {
    public int[] maxSlidingWindow(int[] nums, int k) {
        PriorityQueue 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;
    }
}
using Deque to maintain a window O(n)
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;
        Deque queue = new ArrayDeque<>(); //queue to save index
        for (int i = 0; i < nums.length; i++) {
            //丟棄隊(duì)首那些超出窗口長(zhǎng)度的元素 index < i-k+1
            if (!queue.isEmpty() && queue.peek() < i-k+1) queue.poll();
            //隊(duì)首的元素都是比后來加入元素大的元素,所以存儲(chǔ)的index對(duì)應(yīng)的元素是從小到大
            while (!queue.isEmpty() && nums[queue.peekLast()] < nums[i]) queue.pollLast();
            queue.offer(i);
            if (i >= k-1) res[index++] = nums[queue.peek()];
        }
        return res;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://m.specialneedsforspecialkids.com/yun/72277.html

相關(guān)文章

  • leetcode239. Sliding Window Maximum

    摘要:題目要求假設(shè)有一個(gè)數(shù)組和一個(gè)長(zhǎng)度為的窗口,數(shù)組長(zhǎng)度。當(dāng)窗口右滑時(shí),會(huì)刪除下標(biāo)上的值,并加入下標(biāo)上的值。此時(shí)中記錄的值編程了,并返回當(dāng)前的最大值為。一旦最大值失效,就從窗口中重新找一個(gè)最大值就好了。 題目要求 Given an array nums, there is a sliding window of size k which is moving from the very lef...

    sPeng 評(píng)論0 收藏0
  • LeetCode 之 JavaScript 解答第239題 —— 滑動(dòng)窗口最大值(Sliding W

    摘要:你只可以看到在滑動(dòng)窗口內(nèi)的數(shù)字。滑動(dòng)窗口每次只向右移動(dòng)一位。返回滑動(dòng)窗口最大值。算法思路暴力破解法用兩個(gè)指針,分別指向窗口的起始位置和終止位置,然后遍歷窗口中的數(shù)據(jù),求出最大值向前移動(dòng)兩個(gè)指針,然后操作,直到遍歷數(shù)據(jù)完成位置。 Time:2019/4/16Title: Sliding Window MaximumDifficulty: DifficultyAuthor: 小鹿 題目...

    spacewander 評(píng)論0 收藏0
  • [LintCode/LeetCode] Sliding Window Maximum/Median

    摘要:窗口前進(jìn),刪隊(duì)首元素保證隊(duì)列降序加入當(dāng)前元素下標(biāo)從開始,每一次循環(huán)都將隊(duì)首元素加入結(jié)果數(shù)組 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...

    crelaber 評(píng)論0 收藏0
  • [Leetcode] Sliding Window Maximum 滑動(dòng)窗口最大值

    摘要:這樣,我們可以保證隊(duì)列里的元素是從頭到尾降序的,由于隊(duì)列里只有窗口內(nèi)的數(shù),所以他們其實(shí)就是窗口內(nèi)第一大,第二大,第三大的數(shù)。 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...

    lvzishen 評(píng)論0 收藏0
  • Sliding Window Maximum

    摘要:題目鏈接這道題用,注意一下存的是,因?yàn)橐袛嗍欠竦阶畲蟮闹担峭ㄟ^現(xiàn)在的和第一個(gè)的差來判斷的。 Sliding Window Maximum 題目鏈接:https://leetcode.com/problems... 這道題用deque,注意一下存的是index,因?yàn)橐袛嗍欠竦阶畲蟮膚indow值,是通過現(xiàn)在的index和deque第一個(gè)index的差來判斷的。 public cla...

    mrcode 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<