Leetcode: Sliding Window Maximum

原创 2015年11月19日 16:04:09

Question

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?

Show Hint
Hide Company Tags Zenefits
Hide Tags Heap
Hide Similar Problems (H) Minimum Window Substring (E) Min Stack (H) Longest Substring with At Most Two Distinct Characters (H) Paint House II
Have you met this question in a real interview? Yes No
Discuss


Analysis

The naive way is to maintain a sliding window with size k. For each window, we sort it and get the max value. the time complexity is O( n * nlog(n) )

It is easy to use heap to get the maximum value. But we need to store both its value and its position. The time complexity is O( n* log(n) ).

The efficient solution takes O( n ).
Actually, if using heap, the most of elems in the bottom of heap are never be used. Think about it. Even if the toppest elem is popped, the second largest elem will be appear. The heap size is always k, some elems in the bottom are discarded.

So we use one queue. Instead of storing all elems in current sliding window, it only stores the potential large elems, decreasing subsequence. It is the same as the function of heap.

The first elem of queue is the largest one for current window. When window slides, we need to pop the first elem and add the new one.


Solution

Get idea from here1, here2

time complexity: O(n)
space complexity: O(k)

process

ind:0,  queue: [1],  ans=[]
1  3  -1 -3  5  3  6  7       

ind:1,  queue: [3],  ans=[]
1  3  -1 -3  5  3  6  7    

ind:2,  queue: [3,-1], ans=[3]
1  3  -1 -3  5  3  6  7    

ind:3,  queue: [3,-1,-3], ans=[3,3]
1  3  -1 -3  5  3  6  7 

ind:4,  queue: [5], ans=[3,3,5]
1  3  -1 -3  5  3  6  7 

ind:5,  queue: [5,3], ans=[3,3,5,5]
1  3  -1 -3  5  3  6  7 

ind:6,  queue: [6], ans=[3,3,5,5,6]
1  3  -1 -3  5  3  6  7 

ind:7,  queue: [7], ans=[3,3,5,5,6,7]
1  3  -1 -3  5  3  6  7 

Code

class Solution(object):
    def maxSlidingWindow(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: List[int]
        """

        res, queue = [], []

        for ind in range(len(nums)):
            # make sure the size fo range of all elems in queue is smaller than k  
            if len(queue)>0 and ind-queue[0][0]>=k:
                del queue[0]

            # we don't need to consider the small elem, which are never be the largest one in the range
            while len(queue)>0 and nums[ind] > queue[-1][1]:
                queue.pop()

            queue.append( (ind, nums[ind]) )

            # for each window, output its result
            if ind>=k-1:
                res.append( queue[0][1] )

        return res



版权声明:本文为博主原创文章,未经博主允许不得转载。

leetcode 239: Sliding Window Maximum

leetcode 239: Sliding Window Maximum python c++ java
  • xudli
  • xudli
  • 2015年07月19日 15:41
  • 8706

LeetCode 239. Sliding Window Maximum(滑动窗口最大值)

原题网址:https://leetcode.com/problems/sliding-window-maximum/ Given an array nums, there is a sliding...
  • jmspan
  • jmspan
  • 2016年04月06日 12:53
  • 1717

[python]leetcode(239). Sliding Window Maximum

problem Given an array nums, there is a sliding window of size k which is moving from the very ...
  • PKU_Jade
  • PKU_Jade
  • 2017年09月11日 16:30
  • 119

leetcode 239. Sliding Window Maximum 双端队列 滑动窗口最大值

Given an array nums, there is a sliding window of size k which is moving from the very left of the a...
  • m0_37693059
  • m0_37693059
  • 2017年09月04日 15:25
  • 183

LeetCode:Sliding Window Maximum 题解 Python

LeetCode:Sliding Window Maximum 题解 Python
  • soiliml
  • soiliml
  • 2015年08月19日 15:51
  • 507

Leetcode 480. Sliding Window Median 滑动窗口中的中位数 解题报告

1 解题思想题目会给一个数组,和一个滑动窗口的大小K,让你找出当这个窗口滑动的过程中,这个K的窗口内的中位数分别是多少?最naive的方式就是在k个窗口内排序就好,这里不解释(因为开销很大啊,(n-k...
  • MebiuW
  • MebiuW
  • 2017年01月13日 15:11
  • 2810

[LeetCode]Sliding Window Algorithm相关题目总结【重要】

滑动窗口算法的一些具体应用。subString的问题。https://leetcode.com/problems/find-all-anagrams-in-a-string/discuss/...
  • Ellie_
  • Ellie_
  • 2017年08月06日 17:10
  • 301

239. Sliding Window Maximum&滑动窗口的最大值

题目描述给定一个数组和滑动窗口的大小,找出所有滑动窗口里数值的最大值。例如,如果输入数组{2,3,4,2,6,2,5,1}及滑动窗口的大小3,那么一共存在6个滑动窗口,他们的最大值分别为{4,4,6,...
  • sjt091110317
  • sjt091110317
  • 2016年02月26日 17:02
  • 364

leetcode Sliding Window Maximum

A long array A[] is given to you. There is a sliding window of size w which is moving from the very ...
  • zyfo2
  • zyfo2
  • 2013年04月02日 18:08
  • 1405

[leetcode] Sliding Window Maximum

from : https://leetcode.com/problems/sliding-window-maximum/ Given an array nums, there is a sl...
  • lmxmimihuhu
  • lmxmimihuhu
  • 2015年08月19日 21:01
  • 345
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Leetcode: Sliding Window Maximum
举报原因:
原因补充:

(最多只允许输入30个字)