Skip to content

Commit c70a13d

Browse files
committed
create 0239-sliding-window-maximum.rs
1 parent 794f343 commit c70a13d

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

rust/0239-sliding-window-maximum.rs

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 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

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy