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.
1 parent 04f9b77 commit 0b179aeCopy full SHA for 0b179ae
rust/0293-sliding-window-maximum.rs
@@ -0,0 +1,29 @@
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
+ while r < nums.len() {
10
+ while !q.is_empty() && nums[*q.iter().last().unwrap()] < nums[r] {
11
+ q.pop_back();
12
+ }
13
+ q.push_back(r);
14
15
+ if l > q[0] {
16
+ q.pop_front();
17
18
19
+ if r + 1 >= k as usize {
20
+ output.push(nums[q[0]]);
21
+ l += 1;
22
23
24
+ r += 1;
25
26
27
+ output
28
29
+}
0 commit comments