Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit885350c

Browse files
author
luzhipeng
committed
1 parent6b7b6f9 commit885350c

File tree

5 files changed

+162
-62
lines changed

5 files changed

+162
-62
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ leetcode 题解,记录自己的 leetcode 解题之路。
136136
- 🆕[128.longest-consecutive-sequence](./problems/128.longest-consecutive-sequence.md)
137137
-[145.binary-tree-postorder-traversal](./problems/145.binary-tree-postorder-traversal.md)
138138
-[146.lru-cache](./problems/146.lru-cache.md)
139+
- 🆕[239.sliding-window-maximum](./problems/239.sliding-window-maximum.md)
139140
- 🆕[295.find-median-from-data-stream.md](./problems/295.find-median-from-data-stream.md)
140141
-[301.remove-invalid-parentheses](./problems/301.remove-invalid-parentheses.md)
141142

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<mxfile modified="2019-04-25T03:46:49.398Z" host="www.draw.io" agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36" etag="9O6h0S1KGXXQD0X-YjUP" version="10.6.5" type="device"><diagram id="eWjeoaqTePDURmZ7jabo" name="第 1 页">7Zpdb5swFIZ/DZetwOYrl0nWbJq2qVI3deqdC05ABUzBWdL9+tnFJIFDpk1JaqviKnD8gf28x+QcYwvP8+3HipTJVxbTzEJ2vLXwBwshByFX/EjLS2PxQ7sxrKo0VpX2hrv0N1XGtto6jWndqcgZy3hado0RKwoa8Y6NVBXbdKstWdZ9aklWFBjuIpJB630a86SxhijY2z/RdJW0T3b8SVOSk7aymkmdkJhtDkz4xsLzijHeXOXbOc0kvJZL025xpHQ3sIoW/F8a3N6y7z9KEno/H54+0+nDnK6nV7KB7OYXydZqxmq0/KVFULF1EVPZi23h2SZJOb0rSSRLN0J0YUt4nok7R1zWvGJPdM4yVr22xouFbyNflCzTLBuyqwHQitPt0ak5O2DC0yjLKa9eRJW2QestysmQr+43e8kCZUoO1MITZSTKS1a7rvcgxYVi+R9cHYDVOY3rOSihLqUJhOQNQPIuxQi6HtbPKDCLEQaMrvQ7EjLMkTwISb8nIcM8yQeQPO2MsGGOFBj4RsKG+VEIGPnaGbmG+dEEMAr0MzLMj9qQzegQyfF0U4KBpP5XUp/SLlnTRgmGkgaESf1YEgXo2tMMCsaTJ7pTL6tbhhGNoqE88DH0XM++kAMaQNaFLqh/pfYjdezqXqkDsbp5K1U/Jhitv4d1qp8rjPBPzIKM4OraurmePSswgquHdHM9eyZhBtfQ1cu1Hc97ew8M7Hu/LVcj05XANEowCo/ps7jskxJz5n/7vFKwgvZ8T5lIlq4KcRsJRlTYZ5JgGpFsqgryNI7lYwb5dxVasoIfOPDUD+1ZqOzqg50I684ild9VyvFhVO8OKIUuphQMV0elhpTa/d9rUwpGzKNSg0qFoWalYAw+KjWkFHYnmpWCUf2o1JBSboA0KwXzhFGpQaXaDXVdSmGYeYxKDSnlYZgjvq1SMJcZlRpUaqI5SscIKGUhP+Nq9pY8HdiC8Z/X8sDdTOboy9cTaAcmfyV/K1q3zcVwmh6aklH6/gcFP4C59NtqD3NpoFKL9gt5pNktq1OeMon4kXHO8q5+fRk4K3eqtgdAnSH92JpnaSGUb8+hSmViUic7mQZr1Akp5TDz7UoeoL0mmxrH168nV7/dw30l257NF0g+vy6bTpbpVj4BiH3MKcCG1K7LM7hHf6vFxzDZ8HzoHu0KPr97wM9yp7wainU+vhuOie95vd0b+2L/C+J2f3D6tezg+Dm++QM=</diagram></mxfile>
38.2 KB
Loading
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
##题目地址
2+
3+
https://leetcode.com/problems/sliding-window-maximum/description/
4+
5+
##题目描述
6+
7+
```
8+
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.
9+
10+
Example:
11+
12+
Input: nums = [1,3,-1,-3,5,3,6,7], and k = 3
13+
Output: [3,3,5,5,6,7]
14+
Explanation:
15+
16+
Window position Max
17+
--------------- -----
18+
[1 3 -1] -3 5 3 6 7 3
19+
1 [3 -1 -3] 5 3 6 7 3
20+
1 3 [-1 -3 5] 3 6 7 5
21+
1 3 -1 [-3 5 3] 6 7 5
22+
1 3 -1 -3 [5 3 6] 7 6
23+
1 3 -1 -3 5 [3 6 7] 7
24+
Note:
25+
You may assume k is always valid, 1 ≤ k ≤ input array's size for non-empty array.
26+
27+
Follow up:
28+
Could you solve it in linear time?
29+
```
30+
31+
##思路
32+
33+
符合直觉的想法是直接遍历 nums, 然后然后用一个变量 slideWindow 去承载 k 个元素,
34+
然后对 slideWindow 求最大值,这是可以的,时间复杂度是 O(n\* k).代码如下:
35+
36+
```js
37+
/**
38+
*@param{number[]}nums
39+
*@param{number}k
40+
*@return{number[]}
41+
*/
42+
varmaxSlidingWindow=function(nums,k) {
43+
// bad 时间复杂度O(n * k)
44+
if (nums.length===0|| k===0)return [];
45+
let slideWindow= [];
46+
constret= [];
47+
for (let i=0; i<nums.length- k+1; i++) {
48+
for (let j=0; j< k; j++) {
49+
slideWindow.push(nums[i+ j]);
50+
}
51+
ret.push(Math.max(...slideWindow));
52+
slideWindow= [];
53+
}
54+
return ret;
55+
};
56+
```
57+
58+
但是如果真的是这样,这道题也不会是 hard 吧?这道题有一个 follow up,要求你用线性的时间去完成。
59+
我们可以用双端队列来完成,思路是用一个双端队列来保存`接下来的滑动窗口可能成为最大值的数`。具体做法:
60+
61+
62+
- 入队列
63+
64+
- 移除失效元素,失效元素有两种
65+
66+
1. 一种是已经超出窗口范围了,比如我遍历到第4个元素,k = 3,那么i = 0的元素就不应该出现在双端队列中了
67+
具体就是`索引大于 i - k + 1的元素都应该被清除`
68+
69+
2. 小于当前元素都没有利用价值了,具体就是`从后往前遍历(双端队列是一个递减队列)双端队列,如果小于当前元素就出队列`
70+
71+
72+
如果你仔细观察的话,发现双端队列其实是一个递减的一个队列。因此队首的元素一定是最大的。用图来表示就是:
73+
74+
![239.sliding-window-maximum](../assets/problems/239.sliding-window-maximum.png)
75+
76+
##关键点解析
77+
78+
- 递归简化操作
79+
- 如果树很高,建议使用栈来代替递归
80+
- 这道题目对顺序没要求的,因此队列数组操作都是一样的,无任何区别
81+
82+
##代码
83+
84+
```js
85+
/*
86+
* @lc app=leetcode id=239 lang=javascript
87+
*
88+
* [239] Sliding Window Maximum
89+
*
90+
* https://leetcode.com/problems/sliding-window-maximum/description/
91+
*
92+
* algorithms
93+
* Hard (37.22%)
94+
* Total Accepted: 150.8K
95+
* Total Submissions: 399.5K
96+
* Testcase Example: '[1,3,-1,-3,5,3,6,7]\n3'
97+
*
98+
* Given an array nums, there is a sliding window of size k which is moving
99+
* from the very left of the array to the very right. You can only see the k
100+
* numbers in the window. Each time the sliding window moves right by one
101+
* position. Return the max sliding window.
102+
*
103+
* Example:
104+
*
105+
*
106+
* Input: nums = [1,3,-1,-3,5,3,6,7], and k = 3
107+
* Output: [3,3,5,5,6,7]
108+
* Explanation:
109+
*
110+
* Window position Max
111+
* --------------- -----
112+
* [1 3 -1] -3 5 3 6 7 3
113+
* ⁠1 [3 -1 -3] 5 3 6 7 3
114+
* ⁠1 3 [-1 -3 5] 3 6 7 5
115+
* ⁠1 3 -1 [-3 5 3] 6 7 5
116+
* ⁠1 3 -1 -3 [5 3 6] 7 6
117+
* ⁠1 3 -1 -3 5 [3 6 7] 7
118+
*
119+
*
120+
* Note:
121+
* You may assume k is always valid, 1 ≤ k ≤ input array's size for non-empty
122+
* array.
123+
*
124+
* Follow up:
125+
* Could you solve it in linear time?
126+
*/
127+
/**
128+
*@param{number[]}nums
129+
*@param{number}k
130+
*@return{number[]}
131+
*/
132+
varmaxSlidingWindow=function(nums,k) {
133+
// 双端队列优化时间复杂度, 时间复杂度O(n)
134+
constdeque= [];// 存放在接下来的滑动窗口可能成为最大值的数
135+
constret= [];
136+
for (let i=0; i<nums.length; i++) {
137+
// 清空失效元素
138+
while (deque[0]< i- k+1) {
139+
deque.shift();
140+
}
141+
142+
while (nums[deque[deque.length-1]]< nums[i]) {
143+
deque.pop();
144+
}
145+
146+
deque.push(i);
147+
148+
if (i>= k-1) {
149+
ret.push(nums[deque[0]]);
150+
}
151+
}
152+
return ret;
153+
};
154+
```
155+
156+
##扩展
157+
158+
###为什么用双端队列
159+
因为删除无效元素的时候,会清除队首的元素(索引太小了
160+
)或者队尾(元素太小了)的元素。 因此需要同时对队首和队尾进行操作,使用双端队列是一种合乎情理的做法。

‎todo/slideWindow/239.sliding-window-maximum.js

Lines changed: 0 additions & 62 deletions
This file was deleted.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp