成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

[LintCode/LeetCode] Sliding Window Maximum/Median

crelaber / 2577人閱讀

摘要:窗口前進,刪隊首元素保證隊列降序加入當前元素下標從開始,每一次循環(huán)都將隊首元素加入結(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 from the start of the array, find the maximum number inside the window at each moving.

Example

For array [1, 2, 7, 7, 8], moving window size k = 3. return [7, 7, 8]

At first the window is at the start of the array like this

[|1, 2, 7| ,7, 8] , return the maximum 7;

then the window move one step forward.

[1, |2, 7 ,7|, 8], return the maximum 7;

then the window move one step forward again.

[1, 2, |7, 7, 8|], return the maximum 8;
Challenge

o(n) time and O(k) memory

Solution
public class Solution {
    public ArrayList maxSlidingWindow(int[] nums, int k) {
        Deque dq = new ArrayDeque<>();
        ArrayList res = new ArrayList<>();
        for (int i = 0; i < nums.length; i++) {
            if (!dq.isEmpty() && dq.peekFirst() <= i-k) dq.pollFirst(); //窗口前進,刪隊首元素
            while (!dq.isEmpty() && nums[dq.peekLast()] < nums[i]) dq.pollLast(); //保證隊列降序
            dq.offerLast(i); //加入當前元素下標
            if (i >= k-1) res.add(nums[dq.peekFirst()]); //從k-1開始,每一次循環(huán)都將隊首元素加入結(jié)果數(shù)組
        }
        return res;
    }
}
Sliding Window Median Problem

Given an array of n integer, and a moving window(size k), move the window at each iteration from the start of the array, find the median of the element inside the window at each moving. (If there are even numbers in the array, return the N/2-th number after sorting the element in the window. )

Example

For array [1,2,7,8,5], moving window size k = 3. return [2,7,7]

At first the window is at the start of the array like this

[ | 1,2,7 | ,8,5] , return the median 2;

then the window move one step forward.

[1, | 2,7,8 | ,5], return the median 7;

then the window move one step forward again.

[1,2, | 7,8,5 | ], return the median 7;
Challenge

O(nlog(n)) time

Solution

http://www.jiuzhang.com/solut...

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

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/65077.html

相關(guān)文章

  • [LintCode/LeetCode] Minimum Window Substring

    Problem Given a string source and a string target, find the minimum window in source which will contain all the characters in target. Notice If there is no such window in source that covers all charac...

    Corwien 評論0 收藏0
  • [LintCode/LeetCode] Word Break

    Problem Given a string s and a dictionary of words dict, determine if s can be break into a space-separated sequence of one or more dictionary words. Example Given s = lintcode, dict = [lint, code]. R...

    dunizb 評論0 收藏0
  • [LeetCode] 480. Sliding Window Median

    摘要:存大于的數(shù)存小于的數(shù)保證總比的相等或多一個元素 Problem Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle valu...

    freecode 評論0 收藏0
  • [LintCode/LeetCode] First Unique Character in a S

    Problem Given a string, find the first non-repeating character in it and return its index. If it doesnt exist, return -1. Example Given s = lintcode, return 0. Given s = lovelintcode, return 2. Tags A...

    Xufc 評論0 收藏0
  • [LeetCode] 239. Sliding Window Maximum

    摘要:丟棄隊首那些超出窗口長度的元素隊首的元素都是比后來加入元素大的元素,所以存儲的對應(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...

    lentoo 評論0 收藏0

發(fā)表評論

0條評論

crelaber

|高級講師

TA的文章

閱讀更多
最新活動
閱讀需要支付1元查看
<