Skip to content

Commit 6fe5c7a

Browse files
committed
+ problem 239
1 parent 7003bb3 commit 6fe5c7a

File tree

5 files changed

+171
-0
lines changed

5 files changed

+171
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# 239. Sliding Window Maximum
2+
You are given an array of integers `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.
3+
4+
Return *the max sliding window*.
5+
6+
#### Example 1:
7+
<pre>
8+
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
9+
<strong>Output:</strong> [3,3,5,5,6,7]
10+
<strong>Explanation:</strong>
11+
Window position Max
12+
--------------- -----
13+
[1 3 -1] -3 5 3 6 7 3
14+
1 [3 -1 -3] 5 3 6 7 3
15+
1 3 [-1 -3 5] 3 6 7 5
16+
1 3 -1 [-3 5 3] 6 7 5
17+
1 3 -1 -3 [5 3 6] 7 6
18+
1 3 -1 -3 5 [3 6 7] 7
19+
</pre>
20+
21+
#### Example 2:
22+
<pre>
23+
<strong>Input:</strong> nums = [1], k = 1
24+
<strong>Output:</strong> [1]
25+
</pre>
26+
27+
#### Constraints:
28+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
29+
* <code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code>
30+
* `1 <= k <= nums.length`
31+
32+
## Solutions (Rust)
33+
34+
### 1. Solution
35+
```Rust
36+
use std::collections::VecDeque;
37+
38+
impl Solution {
39+
pub fn max_sliding_window(nums: Vec<i32>, k: i32) -> Vec<i32> {
40+
let k = k as usize;
41+
let mut deque = VecDeque::new();
42+
let mut ret = vec![];
43+
44+
for i in 0..nums.len() {
45+
if i >= k && *deque.front().unwrap_or(&100000) <= i - k {
46+
deque.pop_front();
47+
}
48+
49+
while let Some(&j) = deque.back() {
50+
if nums[j] < nums[i] {
51+
deque.pop_back();
52+
} else {
53+
break;
54+
}
55+
}
56+
57+
deque.push_back(i);
58+
59+
if i >= k - 1 {
60+
ret.push(nums[*deque.front().unwrap()]);
61+
}
62+
}
63+
64+
ret
65+
}
66+
}
67+
```
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# 239. 滑动窗口最大值
2+
给你一个整数数组 `nums`,有一个大小为 `k` 的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口内的 `k` 个数字。滑动窗口每次只向右移动一位。
3+
4+
返回 *滑动窗口中的最大值*
5+
6+
#### 示例 1:
7+
<pre>
8+
<strong>输入:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
9+
<strong>输出:</strong> [3,3,5,5,6,7]
10+
<strong>解释:</strong>
11+
滑动窗口的位置 最大值
12+
--------------- -----
13+
[1 3 -1] -3 5 3 6 7 3
14+
1 [3 -1 -3] 5 3 6 7 3
15+
1 3 [-1 -3 5] 3 6 7 5
16+
1 3 -1 [-3 5 3] 6 7 5
17+
1 3 -1 -3 [5 3 6] 7 6
18+
1 3 -1 -3 5 [3 6 7] 7
19+
</pre>
20+
21+
#### 示例 2:
22+
<pre>
23+
<strong>输入:</strong> nums = [1], k = 1
24+
<strong>输出:</strong> [1]
25+
</pre>
26+
27+
#### 提示:
28+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
29+
* <code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code>
30+
* `1 <= k <= nums.length`
31+
32+
## 题解 (Rust)
33+
34+
### 1. 题解
35+
```Rust
36+
use std::collections::VecDeque;
37+
38+
impl Solution {
39+
pub fn max_sliding_window(nums: Vec<i32>, k: i32) -> Vec<i32> {
40+
let k = k as usize;
41+
let mut deque = VecDeque::new();
42+
let mut ret = vec![];
43+
44+
for i in 0..nums.len() {
45+
if i >= k && *deque.front().unwrap_or(&100000) <= i - k {
46+
deque.pop_front();
47+
}
48+
49+
while let Some(&j) = deque.back() {
50+
if nums[j] < nums[i] {
51+
deque.pop_back();
52+
} else {
53+
break;
54+
}
55+
}
56+
57+
deque.push_back(i);
58+
59+
if i >= k - 1 {
60+
ret.push(nums[*deque.front().unwrap()]);
61+
}
62+
}
63+
64+
ret
65+
}
66+
}
67+
```
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use std::collections::VecDeque;
2+
3+
impl Solution {
4+
pub fn max_sliding_window(nums: Vec<i32>, k: i32) -> Vec<i32> {
5+
let k = k as usize;
6+
let mut deque = VecDeque::new();
7+
let mut ret = vec![];
8+
9+
for i in 0..nums.len() {
10+
if i >= k && *deque.front().unwrap_or(&100000) <= i - k {
11+
deque.pop_front();
12+
}
13+
14+
while let Some(&j) = deque.back() {
15+
if nums[j] < nums[i] {
16+
deque.pop_back();
17+
} else {
18+
break;
19+
}
20+
}
21+
22+
deque.push_back(i);
23+
24+
if i >= k - 1 {
25+
ret.push(nums[*deque.front().unwrap()]);
26+
}
27+
}
28+
29+
ret
30+
}
31+
}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@
168168
[236][236l] |[Lowest Common Ancestor of a Binary Tree][236] |![rb]
169169
[237][237l] |[Delete Node in a Linked List][237] |![py]
170170
[238][238l] |[Product of Array Except Self][238] |![rs]
171+
[239][239l] |[Sliding Window Maximum][239] |![rs]
171172
[240][240l] |[Search a 2D Matrix II][240] |![rb]&nbsp;&nbsp;![kt]
172173
[242][242l] |[Valid Anagram][242] |![rs]
173174
[257][257l] |[Binary Tree Paths][257] |![py]
@@ -1449,6 +1450,7 @@
14491450
[236]:Problemset/0236-Lowest%20Common%20Ancestor%20of%20a%20Binary%20Tree/README.md#236-lowest-common-ancestor-of-a-binary-tree
14501451
[237]:Problemset/0237-Delete%20Node%20in%20a%20Linked%20List/README.md#237-delete-node-in-a-linked-list
14511452
[238]:Problemset/0238-Product%20of%20Array%20Except%20Self/README.md#238-product-of-array-except-self
1453+
[239]:Problemset/0239-Sliding%20Window%20Maximum/README.md#239-sliding-window-maximum
14521454
[240]:Problemset/0240-Search%20a%202D%20Matrix%20II/README.md#240-search-a-2d-matrix-ii
14531455
[242]:Problemset/0242-Valid%20Anagram/README.md#242-valid-anagram
14541456
[257]:Problemset/0257-Binary%20Tree%20Paths/README.md#257-binary-tree-paths
@@ -2727,6 +2729,7 @@
27272729
[236l]:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/
27282730
[237l]:https://leetcode.com/problems/delete-node-in-a-linked-list/
27292731
[238l]:https://leetcode.com/problems/product-of-array-except-self/
2732+
[239l]:https://leetcode.com/problems/sliding-window-maximum/
27302733
[240l]:https://leetcode.com/problems/search-a-2d-matrix-ii/
27312734
[242l]:https://leetcode.com/problems/valid-anagram/
27322735
[257l]:https://leetcode.com/problems/binary-tree-paths/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@
168168
[236][236l] |[二叉树的最近公共祖先][236] |![rb]
169169
[237][237l] |[删除链表中的节点][237] |![py]
170170
[238][238l] |[除自身以外数组的乘积][238] |![rs]
171+
[239][239l] |[滑动窗口最大值][239] |![rs]
171172
[240][240l] |[搜索二维矩阵 II][240] |![rb]&nbsp;&nbsp;![kt]
172173
[242][242l] |[有效的字母异位词][242] |![rs]
173174
[257][257l] |[二叉树的所有路径][257] |![py]
@@ -1449,6 +1450,7 @@
14491450
[236]:Problemset/0236-Lowest%20Common%20Ancestor%20of%20a%20Binary%20Tree/README_CN.md#236-二叉树的最近公共祖先
14501451
[237]:Problemset/0237-Delete%20Node%20in%20a%20Linked%20List/README_CN.md#237-删除链表中的节点
14511452
[238]:Problemset/0238-Product%20of%20Array%20Except%20Self/README_CN.md#238-除自身以外数组的乘积
1453+
[239]:Problemset/0239-Sliding%20Window%20Maximum/README_CN.md#239-滑动窗口最大值
14521454
[240]:Problemset/0240-Search%20a%202D%20Matrix%20II/README_CN.md#240-搜索二维矩阵-ii
14531455
[242]:Problemset/0242-Valid%20Anagram/README_CN.md#242-有效的字母异位词
14541456
[257]:Problemset/0257-Binary%20Tree%20Paths/README_CN.md#257-二叉树的所有路径
@@ -2727,6 +2729,7 @@
27272729
[236l]:https://leetcode.cn/problems/lowest-common-ancestor-of-a-binary-tree/
27282730
[237l]:https://leetcode.cn/problems/delete-node-in-a-linked-list/
27292731
[238l]:https://leetcode.cn/problems/product-of-array-except-self/
2732+
[239l]:https://leetcode.cn/problems/sliding-window-maximum/
27302733
[240l]:https://leetcode.cn/problems/search-a-2d-matrix-ii/
27312734
[242l]:https://leetcode.cn/problems/valid-anagram/
27322735
[257l]:https://leetcode.cn/problems/binary-tree-paths/

0 commit comments

Comments
 (0)