We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 4eec918 + c70a13d commit bd34e31Copy full SHA for bd34e31
rust/0239-sliding-window-maximum.rs
@@ -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 mut output = vec![];
6
+ let mut q: VecDeque<usize> = VecDeque::new();
7
8
+ let (mut l, mut r) = (0, 0);
9
10
+ while r < nums.len() {
11
+ while !q.is_empty() && nums[r] > nums[*q.back().unwrap()] {
12
+ q.pop_back();
13
+ }
14
15
+ q.push_back(r);
16
17
+ if l > *q.front().unwrap() {
18
+ q.pop_front();
19
20
21
+ if r + 1 >= k as usize {
22
+ output.push(nums[*q.front().unwrap()]);
23
+ l += 1;
24
25
26
+ r += 1;
27
28
29
+ output
30
31
+}
0 commit comments