Skip to content

Commit 4b6d9af

Browse files
authored
feat: add rust solution to lc problem: No.0239 (doocs#1156)
1 parent d1f1046 commit 4b6d9af

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

solution/0200-0299/0239.Sliding Window Maximum/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,42 @@ public:
206206
};
207207
```
208208

209+
### **Rust**
210+
211+
```rust
212+
use std::collections::VecDeque;
213+
214+
impl Solution {
215+
#[allow(dead_code)]
216+
pub fn max_sliding_window(nums: Vec<i32>, k: i32) -> Vec<i32> {
217+
// The deque contains the index of `nums`
218+
let mut q: VecDeque<usize> = VecDeque::new();
219+
let mut ans_vec: Vec<i32> = Vec::new();
220+
221+
for i in 0..nums.len() {
222+
// Check the first element of queue, if it's out of bound
223+
if !q.is_empty() && (i as i32 - k + 1) > *q.front().unwrap() as i32 {
224+
// Pop it out
225+
q.pop_front();
226+
}
227+
// Pop back elements out until either the deque is empty
228+
// Or the back element is greater than the current traversed element
229+
while !q.is_empty() && nums[*q.back().unwrap()] <= nums[i] {
230+
q.pop_back();
231+
}
232+
// Push the current index in queue
233+
q.push_back(i);
234+
// Check if the condition is satisfied
235+
if i >= (k - 1) as usize {
236+
ans_vec.push(nums[*q.front().unwrap()]);
237+
}
238+
}
239+
240+
ans_vec
241+
}
242+
}
243+
```
244+
209245
### **Go**
210246

211247
```go

solution/0200-0299/0239.Sliding Window Maximum/README_EN.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,42 @@ public:
171171
};
172172
```
173173

174+
### **Rust**
175+
176+
```rust
177+
use std::collections::VecDeque;
178+
179+
impl Solution {
180+
#[allow(dead_code)]
181+
pub fn max_sliding_window(nums: Vec<i32>, k: i32) -> Vec<i32> {
182+
// The deque contains the index of `nums`
183+
let mut q: VecDeque<usize> = VecDeque::new();
184+
let mut ans_vec: Vec<i32> = Vec::new();
185+
186+
for i in 0..nums.len() {
187+
// Check the first element of queue, if it's out of bound
188+
if !q.is_empty() && (i as i32 - k + 1) > *q.front().unwrap() as i32 {
189+
// Pop it out
190+
q.pop_front();
191+
}
192+
// Pop back elements out until either the deque is empty
193+
// Or the back element is greater than the current traversed element
194+
while !q.is_empty() && nums[*q.back().unwrap()] <= nums[i] {
195+
q.pop_back();
196+
}
197+
// Push the current index in queue
198+
q.push_back(i);
199+
// Check if the condition is satisfied
200+
if i >= (k - 1) as usize {
201+
ans_vec.push(nums[*q.front().unwrap()]);
202+
}
203+
}
204+
205+
ans_vec
206+
}
207+
}
208+
```
209+
174210
### **Go**
175211

176212
```go
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+
#[allow(dead_code)]
5+
pub fn max_sliding_window(nums: Vec<i32>, k: i32) -> Vec<i32> {
6+
// The deque contains the index of `nums`
7+
let mut q: VecDeque<usize> = VecDeque::new();
8+
let mut ans_vec: Vec<i32> = Vec::new();
9+
10+
for i in 0..nums.len() {
11+
// Check the first element of queue, if it's out of bound
12+
if !q.is_empty() && (i as i32 - k + 1) > *q.front().unwrap() as i32 {
13+
// Pop it out
14+
q.pop_front();
15+
}
16+
// Pop back elements out until either the deque is empty
17+
// Or the back element is greater than the current traversed element
18+
while !q.is_empty() && nums[*q.back().unwrap()] <= nums[i] {
19+
q.pop_back();
20+
}
21+
// Push the current index in queue
22+
q.push_back(i);
23+
// Check if the condition is satisfied
24+
if i >= (k - 1) as usize {
25+
ans_vec.push(nums[*q.front().unwrap()]);
26+
}
27+
}
28+
29+
ans_vec
30+
}
31+
}

0 commit comments

Comments
 (0)