File tree Expand file tree Collapse file tree 3 files changed +103
-0
lines changed
solution/0200-0299/0239.Sliding Window Maximum Expand file tree Collapse file tree 3 files changed +103
-0
lines changed Original file line number Diff line number Diff line change @@ -206,6 +206,42 @@ public:
206
206
};
207
207
```
208
208
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
+
209
245
### ** Go**
210
246
211
247
``` go
Original file line number Diff line number Diff line change @@ -171,6 +171,42 @@ public:
171
171
};
172
172
```
173
173
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
+
174
210
### ** Go**
175
211
176
212
``` go
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments