From 25aa9e58c4be3727277f32f6fb4154137050a590 Mon Sep 17 00:00:00 2001 From: Luca Lombardo Date: Sat, 30 Sep 2023 21:55:28 +0200 Subject: [PATCH] readme fixed --- 2023_09_21/README.md | 48 --------------------- 2023_09_21/sliding-window-maximum/README.md | 2 + 2 files changed, 2 insertions(+), 48 deletions(-) delete mode 100644 2023_09_21/README.md diff --git a/2023_09_21/README.md b/2023_09_21/README.md deleted file mode 100644 index ef51426..0000000 --- a/2023_09_21/README.md +++ /dev/null @@ -1,48 +0,0 @@ -> Problem: [Sliding Window Maximum](https://leetcode.com/problems/sliding-window-maximum/) - -# 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, k: i32) -> Vec { - let mut output = Vec::new(); - let mut q: VecDeque = 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) diff --git a/2023_09_21/sliding-window-maximum/README.md b/2023_09_21/sliding-window-maximum/README.md index 17b671c..ef51426 100644 --- a/2023_09_21/sliding-window-maximum/README.md +++ b/2023_09_21/sliding-window-maximum/README.md @@ -1,3 +1,5 @@ +> Problem: [Sliding Window Maximum](https://leetcode.com/problems/sliding-window-maximum/) + # 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.