From 2a3c2c0ba98ac1d256e87bd3b92666a0eb54c8a2 Mon Sep 17 00:00:00 2001 From: Michael Xu Date: Wed, 5 Jul 2023 21:23:22 +0800 Subject: [PATCH] feat: Add rust implementation & Update EN Documentation for NO.1765 --- .../1765.Map of Highest Peak/README.md | 53 ++++++++++++++++ .../1765.Map of Highest Peak/README_EN.md | 63 ++++++++++++++++++- .../1765.Map of Highest Peak/Solution.rs | 48 ++++++++++++++ 3 files changed, 163 insertions(+), 1 deletion(-) create mode 100644 solution/1700-1799/1765.Map of Highest Peak/Solution.rs diff --git a/solution/1700-1799/1765.Map of Highest Peak/README.md b/solution/1700-1799/1765.Map of Highest Peak/README.md index be2282f800a67..22018a5cb5117 100644 --- a/solution/1700-1799/1765.Map of Highest Peak/README.md +++ b/solution/1700-1799/1765.Map of Highest Peak/README.md @@ -260,6 +260,59 @@ public: }; ``` +### **Rust** + +```rust +use std::collections::VecDeque; + +impl Solution { + #[allow(dead_code)] + pub fn highest_peak(is_water: Vec>) -> Vec> { + 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 diff --git a/solution/1700-1799/1765.Map of Highest Peak/README_EN.md b/solution/1700-1799/1765.Map of Highest Peak/README_EN.md index dd8bc4bc0c84d..a529a6788ef26 100644 --- a/solution/1700-1799/1765.Map of Highest Peak/README_EN.md +++ b/solution/1700-1799/1765.Map of Highest Peak/README_EN.md @@ -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. @@ -244,6 +252,59 @@ public: }; ``` +### **Rust** + +```rust +use std::collections::VecDeque; + +impl Solution { + #[allow(dead_code)] + pub fn highest_peak(is_water: Vec>) -> Vec> { + 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 diff --git a/solution/1700-1799/1765.Map of Highest Peak/Solution.rs b/solution/1700-1799/1765.Map of Highest Peak/Solution.rs new file mode 100644 index 0000000000000..754962b16d436 --- /dev/null +++ b/solution/1700-1799/1765.Map of Highest Peak/Solution.rs @@ -0,0 +1,48 @@ +use std::collections::VecDeque; + +impl Solution { + #[allow(dead_code)] + pub fn highest_peak(is_water: Vec>) -> Vec> { + 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 + } +} \ No newline at end of file