Skip to content

feat: Add rust implementation for NO.0239 #1156

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions solution/0200-0299/0239.Sliding Window Maximum/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,42 @@ public:
};
```

### **Rust**

```rust
use std::collections::VecDeque;

impl Solution {
#[allow(dead_code)]
pub fn max_sliding_window(nums: Vec<i32>, k: i32) -> Vec<i32> {
// The deque contains the index of `nums`
let mut q: VecDeque<usize> = VecDeque::new();
let mut ans_vec: Vec<i32> = 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
Expand Down
36 changes: 36 additions & 0 deletions solution/0200-0299/0239.Sliding Window Maximum/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,42 @@ public:
};
```

### **Rust**

```rust
use std::collections::VecDeque;

impl Solution {
#[allow(dead_code)]
pub fn max_sliding_window(nums: Vec<i32>, k: i32) -> Vec<i32> {
// The deque contains the index of `nums`
let mut q: VecDeque<usize> = VecDeque::new();
let mut ans_vec: Vec<i32> = 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
Expand Down
31 changes: 31 additions & 0 deletions solution/0200-0299/0239.Sliding Window Maximum/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use std::collections::VecDeque;

impl Solution {
#[allow(dead_code)]
pub fn max_sliding_window(nums: Vec<i32>, k: i32) -> Vec<i32> {
// The deque contains the index of `nums`
let mut q: VecDeque<usize> = VecDeque::new();
let mut ans_vec: Vec<i32> = 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
}
}