Skip to content

Commit

Permalink
Create 0293-sliding-window-maximum.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
nirajvenkat authored Jan 16, 2023
1 parent 04f9b77 commit 0b179ae
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions rust/0293-sliding-window-maximum.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use std::collections::VecDeque;

impl Solution {
pub fn max_sliding_window(nums: Vec<i32>, k: i32) -> Vec<i32> {
let mut output = vec![];
let mut q: VecDeque<usize> = VecDeque::new();

let (mut l, mut r) = (0, 0);
while r < nums.len() {
while !q.is_empty() && nums[*q.iter().last().unwrap()] < nums[r] {
q.pop_back();
}
q.push_back(r);

if l > q[0] {
q.pop_front();
}

if r + 1 >= k as usize {
output.push(nums[q[0]]);
l += 1;
}

r += 1;
}

output
}
}

0 comments on commit 0b179ae

Please sign in to comment.