sliding window maximum solved

main
Luca Lombardo 1 year ago
parent f54c84ac6a
commit 65cb11fc45

@ -0,0 +1,8 @@
[package]
name = "sliding-window-maximum"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

@ -0,0 +1,46 @@
# Comments on the solution
The problem is to find the maximum value in a sliding window of size `k` over a given array of integers. We can solve this problem using a double-ended queue (deque) to store the indices of the elements in the sliding window. The deque will always store the indices of the elements in decreasing order of their values, with the maximum value at the front of the deque.
We start by initializing an empty deque and two pointers `l` and `r` to the left and right ends of the sliding window. We then iterate over the elements of the array, adding each element to the deque and removing the smaller elements from the back of the deque. We also remove the leftmost element from the deque if it's out of the window. Finally, we append the maximum value to the output if the window size is `k`.
### Complexity analysis
* **Time complexity**: `O(n)`, where `n` is the length of the input array. This is because each element is added to and removed from the deque at most once, and each element is processed by the while loop at most once.
* **Space complexity**: `O(k)`, where `k` is the size of the sliding window, as the deque can store at most `k` elements at any time.
---
### Rust solution
```rust
fn max_sliding_window(nums: Vec<i32>, k: i32) -> Vec<i32> {
let mut output = Vec::new();
let mut q: VecDeque<usize> = VecDeque::new();
let (mut l, mut r) = (0, 0);
while r < nums.len() {
while !q.is_empty() && nums[*q.back().unwrap()] < nums[r] {
q.pop_back();
}
q.push_back(r);
if l > q[0] {
q.pop_front();
}
if (r + 1) as usize >= k as usize {
output.push(nums[*q.front().unwrap()]);
l += 1;
}
r += 1;
}
output
}
```
### Leetcode result
![](https://i.imgur.com/OR15Xve.png)

@ -0,0 +1,34 @@
use std::collections::VecDeque;
fn max_sliding_window(nums: Vec<i32>, k: i32) -> Vec<i32> {
let mut output = Vec::new();
let mut q: VecDeque<usize> = VecDeque::new(); // A double-ended queue implemented with a growable ring buffer.
let (mut l, mut r) = (0, 0);
while r < nums.len() {
// pop smaller values from q
while !q.is_empty() && nums[*q.back().unwrap()] < nums[r] { // q.back() is the index of the min val
q.pop_back(); // remove smaller values
}
q.push_back(r); // push current val to q
// remove left val from window
if l > q[0] { // q[0] is the index of the max val
q.pop_front(); // remove left val
}
if (r + 1) as usize >= k as usize { // window size is k
output.push(nums[*q.front().unwrap()]); // append max val
l += 1; // move left pointer
}
r += 1; // move right pointer
}
output
}
fn main() {
let nums = vec![1, 3, -1, -3, 5, 3, 6, 7];
let k = 3;
let output = max_sliding_window(nums, k);
println!("{:?}", output);
}
Loading…
Cancel
Save