From f580f23dbb66ac1782b897929ba8d95417211dbf Mon Sep 17 00:00:00 2001 From: Michael Xu Date: Thu, 6 Jul 2023 21:40:52 +0800 Subject: [PATCH] feat: Add rust implementation for NO.0239 --- .../0239.Sliding Window Maximum/README.md | 36 +++++++++++++++++++ .../0239.Sliding Window Maximum/README_EN.md | 36 +++++++++++++++++++ .../0239.Sliding Window Maximum/Solution.rs | 31 ++++++++++++++++ 3 files changed, 103 insertions(+) create mode 100644 solution/0200-0299/0239.Sliding Window Maximum/Solution.rs diff --git a/solution/0200-0299/0239.Sliding Window Maximum/README.md b/solution/0200-0299/0239.Sliding Window Maximum/README.md index 1d4045cedbe52..c58551779e872 100644 --- a/solution/0200-0299/0239.Sliding Window Maximum/README.md +++ b/solution/0200-0299/0239.Sliding Window Maximum/README.md @@ -206,6 +206,42 @@ public: }; ``` +### **Rust** + +```rust +use std::collections::VecDeque; + +impl Solution { + #[allow(dead_code)] + pub fn max_sliding_window(nums: Vec, k: i32) -> Vec { + // The deque contains the index of `nums` + let mut q: VecDeque = VecDeque::new(); + let mut ans_vec: Vec = Vec::new(); + + for i in 0..nums.len() { + // Check the first element of queue, if it's out of bound + if !q.is_empty() && (i as i32 - k + 1) > *q.front().unwrap() as i32 { + // Pop it out + q.pop_front(); + } + // Pop back elements out until either the deque is empty + // Or the back element is greater than the current traversed element + while !q.is_empty() && nums[*q.back().unwrap()] <= nums[i] { + q.pop_back(); + } + // Push the current index in queue + q.push_back(i); + // Check if the condition is satisfied + if i >= (k - 1) as usize { + ans_vec.push(nums[*q.front().unwrap()]); + } + } + + ans_vec + } +} +``` + ### **Go** ```go diff --git a/solution/0200-0299/0239.Sliding Window Maximum/README_EN.md b/solution/0200-0299/0239.Sliding Window Maximum/README_EN.md index 5438d3a1bc45b..ef67e4fe6f677 100644 --- a/solution/0200-0299/0239.Sliding Window Maximum/README_EN.md +++ b/solution/0200-0299/0239.Sliding Window Maximum/README_EN.md @@ -171,6 +171,42 @@ public: }; ``` +### **Rust** + +```rust +use std::collections::VecDeque; + +impl Solution { + #[allow(dead_code)] + pub fn max_sliding_window(nums: Vec, k: i32) -> Vec { + // The deque contains the index of `nums` + let mut q: VecDeque = VecDeque::new(); + let mut ans_vec: Vec = Vec::new(); + + for i in 0..nums.len() { + // Check the first element of queue, if it's out of bound + if !q.is_empty() && (i as i32 - k + 1) > *q.front().unwrap() as i32 { + // Pop it out + q.pop_front(); + } + // Pop back elements out until either the deque is empty + // Or the back element is greater than the current traversed element + while !q.is_empty() && nums[*q.back().unwrap()] <= nums[i] { + q.pop_back(); + } + // Push the current index in queue + q.push_back(i); + // Check if the condition is satisfied + if i >= (k - 1) as usize { + ans_vec.push(nums[*q.front().unwrap()]); + } + } + + ans_vec + } +} +``` + ### **Go** ```go diff --git a/solution/0200-0299/0239.Sliding Window Maximum/Solution.rs b/solution/0200-0299/0239.Sliding Window Maximum/Solution.rs new file mode 100644 index 0000000000000..d5043304e179d --- /dev/null +++ b/solution/0200-0299/0239.Sliding Window Maximum/Solution.rs @@ -0,0 +1,31 @@ +use std::collections::VecDeque; + +impl Solution { + #[allow(dead_code)] + pub fn max_sliding_window(nums: Vec, k: i32) -> Vec { + // The deque contains the index of `nums` + let mut q: VecDeque = VecDeque::new(); + let mut ans_vec: Vec = Vec::new(); + + for i in 0..nums.len() { + // Check the first element of queue, if it's out of bound + if !q.is_empty() && (i as i32 - k + 1) > *q.front().unwrap() as i32 { + // Pop it out + q.pop_front(); + } + // Pop back elements out until either the deque is empty + // Or the back element is greater than the current traversed element + while !q.is_empty() && nums[*q.back().unwrap()] <= nums[i] { + q.pop_back(); + } + // Push the current index in queue + q.push_back(i); + // Check if the condition is satisfied + if i >= (k - 1) as usize { + ans_vec.push(nums[*q.front().unwrap()]); + } + } + + ans_vec + } +} \ No newline at end of file