Skip to content

Commit 00b1a55

Browse files
authored
feat: add rust solution to lc problem: No.1765 (#1148)
1 parent da1778f commit 00b1a55

File tree

3 files changed

+163
-1
lines changed

3 files changed

+163
-1
lines changed

solution/1700-1799/1765.Map of Highest Peak/README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,59 @@ public:
260260
};
261261
```
262262

263+
### **Rust**
264+
265+
```rust
266+
use std::collections::VecDeque;
267+
268+
impl Solution {
269+
#[allow(dead_code)]
270+
pub fn highest_peak(is_water: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
271+
let n = is_water.len();
272+
let m = is_water[0].len();
273+
let mut ret_vec = vec![vec![-1; m]; n];
274+
let mut q: VecDeque<(usize, usize)> = VecDeque::new();
275+
let vis_pair: Vec<(i32, i32)> = vec![(-1, 0), (1, 0), (0, -1), (0 ,1)];
276+
277+
// Initialize the return vector
278+
for i in 0..n {
279+
for j in 0..m {
280+
if is_water[i][j] == 1 {
281+
// This cell is water, the height of which must be 0
282+
ret_vec[i][j] = 0;
283+
q.push_back((i, j));
284+
}
285+
}
286+
}
287+
288+
while !q.is_empty() {
289+
// Get the front X-Y Coordinates
290+
let (x, y) = q.front().unwrap().clone();
291+
q.pop_front();
292+
// Traverse through the vis pair
293+
for d in &vis_pair {
294+
let (dx, dy) = *d;
295+
if Self::check_bounds(x as i32 + dx, y as i32 + dy, n as i32, m as i32) {
296+
if ret_vec[(x as i32 + dx) as usize][(y as i32 + dy) as usize] == -1 {
297+
// This cell hasn't been visited, update its height
298+
ret_vec[(x as i32 + dx) as usize][(y as i32 + dy) as usize] = ret_vec[x][y] + 1;
299+
// Enqueue the current cell
300+
q.push_back(((x as i32 + dx) as usize, (y as i32 + dy) as usize));
301+
}
302+
}
303+
}
304+
}
305+
306+
ret_vec
307+
}
308+
309+
#[allow(dead_code)]
310+
fn check_bounds(i: i32, j: i32, n: i32, m: i32) -> bool {
311+
i >= 0 && i < n && j >= 0 && j < m
312+
}
313+
}
314+
```
315+
263316
### **Go**
264317

265318
```go

solution/1700-1799/1765.Map of Highest Peak/README_EN.md

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,15 @@ Any height assignment that has a maximum height of 2 while still meeting the rul
5959

6060
## Solutions
6161

62-
BFS.
62+
**Method One: Multi-Source BFS**
63+
64+
Based on the problem description, the height of the water area must be $0$, and the height difference between any adjacent cells can be at most $1$.
65+
66+
Therefore, we can start from all water cells, perform BFS to search for adjacent and unvisited cells, and set their heights to the height of the current cell plus one.
67+
68+
Finally, return the resulting matrix.
69+
70+
The time complexity is $O(m \times n)$, and the space complexity is $O(m \times n)$, where $m$ and $n$ are the number of rows and columns in the integer matrix isWater, respectively.
6371

6472
<!-- tabs:start -->
6573

@@ -244,6 +252,59 @@ public:
244252
};
245253
```
246254

255+
### **Rust**
256+
257+
```rust
258+
use std::collections::VecDeque;
259+
260+
impl Solution {
261+
#[allow(dead_code)]
262+
pub fn highest_peak(is_water: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
263+
let n = is_water.len();
264+
let m = is_water[0].len();
265+
let mut ret_vec = vec![vec![-1; m]; n];
266+
let mut q: VecDeque<(usize, usize)> = VecDeque::new();
267+
let vis_pair: Vec<(i32, i32)> = vec![(-1, 0), (1, 0), (0, -1), (0 ,1)];
268+
269+
// Initialize the return vector
270+
for i in 0..n {
271+
for j in 0..m {
272+
if is_water[i][j] == 1 {
273+
// This cell is water, the height of which must be 0
274+
ret_vec[i][j] = 0;
275+
q.push_back((i, j));
276+
}
277+
}
278+
}
279+
280+
while !q.is_empty() {
281+
// Get the front X-Y Coordinates
282+
let (x, y) = q.front().unwrap().clone();
283+
q.pop_front();
284+
// Traverse through the vis pair
285+
for d in &vis_pair {
286+
let (dx, dy) = *d;
287+
if Self::check_bounds(x as i32 + dx, y as i32 + dy, n as i32, m as i32) {
288+
if ret_vec[(x as i32 + dx) as usize][(y as i32 + dy) as usize] == -1 {
289+
// This cell hasn't been visited, update its height
290+
ret_vec[(x as i32 + dx) as usize][(y as i32 + dy) as usize] = ret_vec[x][y] + 1;
291+
// Enqueue the current cell
292+
q.push_back(((x as i32 + dx) as usize, (y as i32 + dy) as usize));
293+
}
294+
}
295+
}
296+
}
297+
298+
ret_vec
299+
}
300+
301+
#[allow(dead_code)]
302+
fn check_bounds(i: i32, j: i32, n: i32, m: i32) -> bool {
303+
i >= 0 && i < n && j >= 0 && j < m
304+
}
305+
}
306+
```
307+
247308
### **Go**
248309

249310
```go
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use std::collections::VecDeque;
2+
3+
impl Solution {
4+
#[allow(dead_code)]
5+
pub fn highest_peak(is_water: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
6+
let n = is_water.len();
7+
let m = is_water[0].len();
8+
let mut ret_vec = vec![vec![-1; m]; n];
9+
let mut q: VecDeque<(usize, usize)> = VecDeque::new();
10+
let vis_pair: Vec<(i32, i32)> = vec![(-1, 0), (1, 0), (0, -1), (0 ,1)];
11+
12+
// Initialize the return vector
13+
for i in 0..n {
14+
for j in 0..m {
15+
if is_water[i][j] == 1 {
16+
// This cell is water, the height of which must be 0
17+
ret_vec[i][j] = 0;
18+
q.push_back((i, j));
19+
}
20+
}
21+
}
22+
23+
while !q.is_empty() {
24+
// Get the front X-Y Coordinates
25+
let (x, y) = q.front().unwrap().clone();
26+
q.pop_front();
27+
// Traverse through the vis pair
28+
for d in &vis_pair {
29+
let (dx, dy) = *d;
30+
if Self::check_bounds(x as i32 + dx, y as i32 + dy, n as i32, m as i32) {
31+
if ret_vec[(x as i32 + dx) as usize][(y as i32 + dy) as usize] == -1 {
32+
// This cell hasn't been visited, update its height
33+
ret_vec[(x as i32 + dx) as usize][(y as i32 + dy) as usize] = ret_vec[x][y] + 1;
34+
// Enqueue the current cell
35+
q.push_back(((x as i32 + dx) as usize, (y as i32 + dy) as usize));
36+
}
37+
}
38+
}
39+
}
40+
41+
ret_vec
42+
}
43+
44+
#[allow(dead_code)]
45+
fn check_bounds(i: i32, j: i32, n: i32, m: i32) -> bool {
46+
i >= 0 && i < n && j >= 0 && j < m
47+
}
48+
}

0 commit comments

Comments
 (0)