Leetcode Hot 100 & 239. Sliding Window Maximum

  References:

  Python documentation heapq section

  Test point: substring & [ question stem ]

 1 Input: nums = [1,3,-1,-3,5,3,6,7], k = 3
 2 Output: [3,3,5,5,6,7]
 3 Explanation: 
 4 Window position                Max
 5 ---------------               -----
 6 [1  3  -1] -3  5  3  6  7       3
 7  1 [3  -1  -3] 5  3  6  7       3
 8  1  3 [-1  -3  5] 3  6  7       5
 9  1  3  -1 [-3  5  3] 6  7       5
10  1  3  -1  -3 [5  3  6] 7       6
11  1  3  -1  -3  5 [3  6  7]      7

  1. Mental journey

  When I saw this question at first glance, I thought about it, hey~ Isn’t this just one-dimensional maximum pooling? Is there any good optimization algorithm for this? The first thing that comes into mind is an algorithm that must time out, as follows:

  For each sliding of the window, since k is fixed, each value in the window can be directly indexed, and the maximum value of the for loop must be the correct result. But this method is sure to time out, because leetcode officials can set k to be relatively large.

  An optimization idea:

  Inspired by the previous question, we can actually get more information about substrings in one traversal. Let the prefix max value be pre_max, then max( pre_max[i], pre_max[i+1:j] ) = pre_max[j] . This is easier to understand. If the maximum value of the first i values ​​is calculated as A, and the maximum value of the i+1~j substring is B, then the maximum value of the first j values ​​must be the maximum value between the two. If pre_max[j] == pre_max[i], then the value of pre_max[i+1:j] is still unknown, if pre_max[j] > pre_max[i], then the value of the substring is equal to pre_max[j]. This shallow optimization is useful for increasing sequences, but completely useless for decreasing sequences. So this optimization method is not enough to keep the algorithm from timing out.

  2. Correct

  If I can’t think of a good solution later, I will just read the official solution. After reading it, I just want to say that some of the questions on leetcode are not pure. For example, the sorting algorithm is purely playing with arrays. But doing leetcode requires you to be experienced and call a lot of builtin packages (that is, use a lot of built-in written data structures) to complete. For example, this question uses a tree-shaped data structure such as a priority queue, that is, a large root heap. Taking the python language as an example, if you are not familiar with (or don’t use) heapq, a built-in priority queue implementation package, this question is probably right for you. invisible barriers. After talking about the data structure, let me analyze the idea of ​​the official solution. In fact, I just use the data structure of Dagendui to simplify the operation of judging the maximum value of each substring in the mental journey . First put the first k numbers into the big root pile, so that the largest value is on the root, and then move to the right to scan one bit, so that there are k+1 numbers in the big root pile. Then judge, that is, whether the current root node is in the current sliding window, if not, pop it out, if it is, then the root is the maximum value of the current window. For this purpose, use a tuple record (value, index), and then use the big root heap to realize it perfectly. To understand the problem-solving ideas and tools, the following is your own implementation:

 1 class Solution(object):
 2     def maxSlidingWindow(self, nums, k):
 3         """
 4         :type nums: List[int]
 5         :type k: int
 6         :rtype: List[int]
 7         """
 8         n = len(nums)
 9 
10         myheap = []
11         result = []
12 
13         for i in range(k):
14             heappush(myheap, (-nums[i], i))
15             
16         result.append(-myheap[0][0])
17 
18         for i in range(k, n):
19             heappush(myheap, (-nums[i], i))
20             while myheap[0][1] <= i - k:
21                 heappop(myheap)
22             result.append(-myheap[0][0])
23 
24         return result

おすすめ

転載: blog.csdn.net/weixin_43590796/article/details/131279776