Skip to content

Commit 2cb2943

Browse files
authored
feat: add rust solution to lc problem: No.0778 (#1179)
1 parent e4ba68b commit 2cb2943

File tree

3 files changed

+220
-0
lines changed

3 files changed

+220
-0
lines changed

solution/0700-0799/0778.Swim in Rising Water/README.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,81 @@ public:
268268
};
269269
```
270270
271+
### **Rust**
272+
273+
```rust
274+
const DIR: [(i32, i32); 4] = [(-1, 0), (1, 0), (0, -1), (0, 1)];
275+
276+
impl Solution {
277+
#[allow(dead_code)]
278+
pub fn swim_in_water(grid: Vec<Vec<i32>>) -> i32 {
279+
let n = grid.len();
280+
let m = grid[0].len();
281+
let mut ret_time = 0;
282+
let mut disjoint_set: Vec<usize> = vec![0; n * m];
283+
284+
// Initialize the disjoint set
285+
for i in 0..n * m {
286+
disjoint_set[i] = i;
287+
}
288+
289+
loop {
290+
if Self::check_and_union(&grid, &mut disjoint_set, ret_time) {
291+
break;
292+
}
293+
// Otherwise, keep checking
294+
ret_time += 1;
295+
}
296+
297+
ret_time
298+
}
299+
300+
#[allow(dead_code)]
301+
fn check_and_union(grid: &Vec<Vec<i32>>, d_set: &mut Vec<usize>, cur_time: i32) -> bool {
302+
let n = grid.len();
303+
let m = grid[0].len();
304+
305+
for i in 0..n {
306+
for j in 0..m {
307+
if grid[i][j] != cur_time {
308+
continue;
309+
}
310+
// Otherwise, let's union the square with its neighbors
311+
for (dx, dy) in DIR {
312+
let x = dx + i as i32;
313+
let y = dy + j as i32;
314+
if Self::check_bounds(x, y, n as i32, m as i32) && grid[x as usize][y as usize] <= cur_time {
315+
Self::union(i * m + j, x as usize * m + y as usize, d_set);
316+
}
317+
}
318+
}
319+
}
320+
321+
Self::find(0, d_set) == Self::find(n * m - 1, d_set)
322+
}
323+
324+
#[allow(dead_code)]
325+
fn find(x: usize, d_set: &mut Vec<usize>) -> usize {
326+
if d_set[x] != x {
327+
d_set[x] = Self::find(d_set[x], d_set);
328+
}
329+
d_set[x]
330+
}
331+
332+
#[allow(dead_code)]
333+
fn union(x: usize, y: usize, d_set: &mut Vec<usize>) {
334+
let p_x = Self::find(x, d_set);
335+
let p_y = Self::find(y, d_set);
336+
d_set[p_x] = p_y;
337+
}
338+
339+
#[allow(dead_code)]
340+
fn check_bounds(i: i32, j: i32, n: i32, m: i32) -> bool {
341+
i >= 0 && i < n && j >= 0 && j < m
342+
}
343+
}
344+
```
345+
271346
### **Go**
272347

273348
```go

solution/0700-0799/0778.Swim in Rising Water/README_EN.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,81 @@ public:
188188
};
189189
```
190190
191+
### **Rust**
192+
193+
```rust
194+
const DIR: [(i32, i32); 4] = [(-1, 0), (1, 0), (0, -1), (0, 1)];
195+
196+
impl Solution {
197+
#[allow(dead_code)]
198+
pub fn swim_in_water(grid: Vec<Vec<i32>>) -> i32 {
199+
let n = grid.len();
200+
let m = grid[0].len();
201+
let mut ret_time = 0;
202+
let mut disjoint_set: Vec<usize> = vec![0; n * m];
203+
204+
// Initialize the disjoint set
205+
for i in 0..n * m {
206+
disjoint_set[i] = i;
207+
}
208+
209+
loop {
210+
if Self::check_and_union(&grid, &mut disjoint_set, ret_time) {
211+
break;
212+
}
213+
// Otherwise, keep checking
214+
ret_time += 1;
215+
}
216+
217+
ret_time
218+
}
219+
220+
#[allow(dead_code)]
221+
fn check_and_union(grid: &Vec<Vec<i32>>, d_set: &mut Vec<usize>, cur_time: i32) -> bool {
222+
let n = grid.len();
223+
let m = grid[0].len();
224+
225+
for i in 0..n {
226+
for j in 0..m {
227+
if grid[i][j] != cur_time {
228+
continue;
229+
}
230+
// Otherwise, let's union the square with its neighbors
231+
for (dx, dy) in DIR {
232+
let x = dx + i as i32;
233+
let y = dy + j as i32;
234+
if Self::check_bounds(x, y, n as i32, m as i32) && grid[x as usize][y as usize] <= cur_time {
235+
Self::union(i * m + j, x as usize * m + y as usize, d_set);
236+
}
237+
}
238+
}
239+
}
240+
241+
Self::find(0, d_set) == Self::find(n * m - 1, d_set)
242+
}
243+
244+
#[allow(dead_code)]
245+
fn find(x: usize, d_set: &mut Vec<usize>) -> usize {
246+
if d_set[x] != x {
247+
d_set[x] = Self::find(d_set[x], d_set);
248+
}
249+
d_set[x]
250+
}
251+
252+
#[allow(dead_code)]
253+
fn union(x: usize, y: usize, d_set: &mut Vec<usize>) {
254+
let p_x = Self::find(x, d_set);
255+
let p_y = Self::find(y, d_set);
256+
d_set[p_x] = p_y;
257+
}
258+
259+
#[allow(dead_code)]
260+
fn check_bounds(i: i32, j: i32, n: i32, m: i32) -> bool {
261+
i >= 0 && i < n && j >= 0 && j < m
262+
}
263+
}
264+
```
265+
191266
### **Go**
192267

193268
```go
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
const DIR: [(i32, i32); 4] = [(-1, 0), (1, 0), (0, -1), (0, 1)];
2+
3+
impl Solution {
4+
#[allow(dead_code)]
5+
pub fn swim_in_water(grid: Vec<Vec<i32>>) -> i32 {
6+
let n = grid.len();
7+
let m = grid[0].len();
8+
let mut ret_time = 0;
9+
let mut disjoint_set: Vec<usize> = vec![0; n * m];
10+
11+
// Initialize the disjoint set
12+
for i in 0..n * m {
13+
disjoint_set[i] = i;
14+
}
15+
16+
loop {
17+
if Self::check_and_union(&grid, &mut disjoint_set, ret_time) {
18+
break;
19+
}
20+
// Otherwise, keep checking
21+
ret_time += 1;
22+
}
23+
24+
ret_time
25+
}
26+
27+
#[allow(dead_code)]
28+
fn check_and_union(grid: &Vec<Vec<i32>>, d_set: &mut Vec<usize>, cur_time: i32) -> bool {
29+
let n = grid.len();
30+
let m = grid[0].len();
31+
32+
for i in 0..n {
33+
for j in 0..m {
34+
if grid[i][j] != cur_time {
35+
continue;
36+
}
37+
// Otherwise, let's union the square with its neighbors
38+
for (dx, dy) in DIR {
39+
let x = dx + i as i32;
40+
let y = dy + j as i32;
41+
if Self::check_bounds(x, y, n as i32, m as i32) && grid[x as usize][y as usize] <= cur_time {
42+
Self::union(i * m + j, x as usize * m + y as usize, d_set);
43+
}
44+
}
45+
}
46+
}
47+
48+
Self::find(0, d_set) == Self::find(n * m - 1, d_set)
49+
}
50+
51+
#[allow(dead_code)]
52+
fn find(x: usize, d_set: &mut Vec<usize>) -> usize {
53+
if d_set[x] != x {
54+
d_set[x] = Self::find(d_set[x], d_set);
55+
}
56+
d_set[x]
57+
}
58+
59+
#[allow(dead_code)]
60+
fn union(x: usize, y: usize, d_set: &mut Vec<usize>) {
61+
let p_x = Self::find(x, d_set);
62+
let p_y = Self::find(y, d_set);
63+
d_set[p_x] = p_y;
64+
}
65+
66+
#[allow(dead_code)]
67+
fn check_bounds(i: i32, j: i32, n: i32, m: i32) -> bool {
68+
i >= 0 && i < n && j >= 0 && j < m
69+
}
70+
}

0 commit comments

Comments
 (0)