算法分析课每周练习 Sliding Window Maximum
2017-06-26 23:41
357 查看
题目
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 the very right. You can only see the k numbers
in the window. Each time the sliding window moves right by one position.
分析
传说中的单调队列
from collections import deque
class Solution:
"""
@param nums: A list of integers.
@return: The maximum number inside the window at each moving.
"""
def maxSlidingWindow(self, nums, k):
# write your code here
q = deque()
result = []
if len(nums) < k or k == 0:
return []
n = len(nums)
for i in xrange(n):
while len(q) and nums[q[-1]] < nums[i]:
q.pop()
q.append(i)
if i < k - 1:
continue
while len(q) and q[0] <= i - k:
q.popleft()
re
4000
sult.append(nums[q[0]])
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 the very right. You can only see the k numbers
in the window. Each time the sliding window moves right by one position.
分析
传说中的单调队列
from collections import deque
class Solution:
"""
@param nums: A list of integers.
@return: The maximum number inside the window at each moving.
"""
def maxSlidingWindow(self, nums, k):
# write your code here
q = deque()
result = []
if len(nums) < k or k == 0:
return []
n = len(nums)
for i in xrange(n):
while len(q) and nums[q[-1]] < nums[i]:
q.pop()
q.append(i)
if i < k - 1:
continue
while len(q) and q[0] <= i - k:
q.popleft()
re
4000
sult.append(nums[q[0]])
相关文章推荐
- 算法分析课每周练习 Binary Tree Maximum Path Sum
- 算法分析课每周练习 Minimum Window Substring
- 算法分析课每周练习 Wildcard Matching
- 算法分析课每周练习 First Missing Positive
- 算法分析课每周练习 Median of Two Sorted Arrays
- 算法分析课每周练习 Trapping Rain Water
- 算法分析课每周练习 Regular Expression Matching
- 算法分析课每周练习 Count of Smaller Numbers After Self
- 算法分析课每周练习 Max Points on a Line
- 算法分析课每周练习 Merge k Sorted Lists
- 算法分析课每周练习 Serialize and Deserialize Binary Tree
- 算法分析课每周练习 The Skyline Problem
- 算法分析课每周练习 Find Median from Data Stream
- 算法分析课每周练习 Word Search II
- 算法分析课每周练习 LRU Cache
- 【原创】Sliding Window Maximum 解法分析
- 算法分析课每周练习 Longest Increasing Path in a Matrix
- Leetcode 239 Sliding Window Maximum
- leetcode - Sliding Window Maximum
- [LeetCode] Sliding Window Maximum