Skip to content
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
53 changes: 53 additions & 0 deletions solution/1700-1799/1765.Map of Highest Peak/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,59 @@ public:
};
```

### **Rust**

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

impl Solution {
#[allow(dead_code)]
pub fn highest_peak(is_water: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
let n = is_water.len();
let m = is_water[0].len();
let mut ret_vec = vec![vec![-1; m]; n];
let mut q: VecDeque<(usize, usize)> = VecDeque::new();
let vis_pair: Vec<(i32, i32)> = vec![(-1, 0), (1, 0), (0, -1), (0 ,1)];

// Initialize the return vector
for i in 0..n {
for j in 0..m {
if is_water[i][j] == 1 {
// This cell is water, the height of which must be 0
ret_vec[i][j] = 0;
q.push_back((i, j));
}
}
}

while !q.is_empty() {
// Get the front X-Y Coordinates
let (x, y) = q.front().unwrap().clone();
q.pop_front();
// Traverse through the vis pair
for d in &vis_pair {
let (dx, dy) = *d;
if Self::check_bounds(x as i32 + dx, y as i32 + dy, n as i32, m as i32) {
if ret_vec[(x as i32 + dx) as usize][(y as i32 + dy) as usize] == -1 {
// This cell hasn't been visited, update its height
ret_vec[(x as i32 + dx) as usize][(y as i32 + dy) as usize] = ret_vec[x][y] + 1;
// Enqueue the current cell
q.push_back(((x as i32 + dx) as usize, (y as i32 + dy) as usize));
}
}
}
}

ret_vec
}

#[allow(dead_code)]
fn check_bounds(i: i32, j: i32, n: i32, m: i32) -> bool {
i >= 0 && i < n && j >= 0 && j < m
}
}
```

### **Go**

```go
Expand Down
63 changes: 62 additions & 1 deletion solution/1700-1799/1765.Map of Highest Peak/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,15 @@ Any height assignment that has a maximum height of 2 while still meeting the rul

## Solutions

BFS.
**Method One: Multi-Source BFS**

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$.

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.

Finally, return the resulting matrix.

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.

<!-- tabs:start -->

Expand Down Expand Up @@ -244,6 +252,59 @@ public:
};
```

### **Rust**

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

impl Solution {
#[allow(dead_code)]
pub fn highest_peak(is_water: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
let n = is_water.len();
let m = is_water[0].len();
let mut ret_vec = vec![vec![-1; m]; n];
let mut q: VecDeque<(usize, usize)> = VecDeque::new();
let vis_pair: Vec<(i32, i32)> = vec![(-1, 0), (1, 0), (0, -1), (0 ,1)];

// Initialize the return vector
for i in 0..n {
for j in 0..m {
if is_water[i][j] == 1 {
// This cell is water, the height of which must be 0
ret_vec[i][j] = 0;
q.push_back((i, j));
}
}
}

while !q.is_empty() {
// Get the front X-Y Coordinates
let (x, y) = q.front().unwrap().clone();
q.pop_front();
// Traverse through the vis pair
for d in &vis_pair {
let (dx, dy) = *d;
if Self::check_bounds(x as i32 + dx, y as i32 + dy, n as i32, m as i32) {
if ret_vec[(x as i32 + dx) as usize][(y as i32 + dy) as usize] == -1 {
// This cell hasn't been visited, update its height
ret_vec[(x as i32 + dx) as usize][(y as i32 + dy) as usize] = ret_vec[x][y] + 1;
// Enqueue the current cell
q.push_back(((x as i32 + dx) as usize, (y as i32 + dy) as usize));
}
}
}
}

ret_vec
}

#[allow(dead_code)]
fn check_bounds(i: i32, j: i32, n: i32, m: i32) -> bool {
i >= 0 && i < n && j >= 0 && j < m
}
}
```

### **Go**

```go
Expand Down
48 changes: 48 additions & 0 deletions solution/1700-1799/1765.Map of Highest Peak/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use std::collections::VecDeque;

impl Solution {
#[allow(dead_code)]
pub fn highest_peak(is_water: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
let n = is_water.len();
let m = is_water[0].len();
let mut ret_vec = vec![vec![-1; m]; n];
let mut q: VecDeque<(usize, usize)> = VecDeque::new();
let vis_pair: Vec<(i32, i32)> = vec![(-1, 0), (1, 0), (0, -1), (0 ,1)];

// Initialize the return vector
for i in 0..n {
for j in 0..m {
if is_water[i][j] == 1 {
// This cell is water, the height of which must be 0
ret_vec[i][j] = 0;
q.push_back((i, j));
}
}
}

while !q.is_empty() {
// Get the front X-Y Coordinates
let (x, y) = q.front().unwrap().clone();
q.pop_front();
// Traverse through the vis pair
for d in &vis_pair {
let (dx, dy) = *d;
if Self::check_bounds(x as i32 + dx, y as i32 + dy, n as i32, m as i32) {
if ret_vec[(x as i32 + dx) as usize][(y as i32 + dy) as usize] == -1 {
// This cell hasn't been visited, update its height
ret_vec[(x as i32 + dx) as usize][(y as i32 + dy) as usize] = ret_vec[x][y] + 1;
// Enqueue the current cell
q.push_back(((x as i32 + dx) as usize, (y as i32 + dy) as usize));
}
}
}
}

ret_vec
}

#[allow(dead_code)]
fn check_bounds(i: i32, j: i32, n: i32, m: i32) -> bool {
i >= 0 && i < n && j >= 0 && j < m
}
}