From bcfe2e6c4a11e18fa27274002d68a40b796cd79b Mon Sep 17 00:00:00 2001 From: YangFong Date: Sun, 20 Aug 2023 09:50:01 +0800 Subject: [PATCH] feat: add solutions to lc problems: No.2810~2812 - No.2810.Faulty Keyboard - No.2811.Check if it is Possible to Split Array - No.2812.Find the Safest Path in a Grid --- .../2800-2899/2810.Faulty Keyboard/README.md | 18 +++ .../2810.Faulty Keyboard/README_EN.md | 18 +++ .../2810.Faulty Keyboard/Solution.rs | 13 ++ .../README.md | 42 ++++++ .../README_EN.md | 34 +++++ .../Solution.rs | 14 ++ .../README.md | 126 ++++++++++++++++++ .../README_EN.md | 126 ++++++++++++++++++ .../Solution.rs | 63 +++++++++ 9 files changed, 454 insertions(+) create mode 100644 solution/2800-2899/2810.Faulty Keyboard/Solution.rs create mode 100644 solution/2800-2899/2811.Check if it is Possible to Split Array/Solution.rs create mode 100644 solution/2800-2899/2812.Find the Safest Path in a Grid/Solution.rs diff --git a/solution/2800-2899/2810.Faulty Keyboard/README.md b/solution/2800-2899/2810.Faulty Keyboard/README.md index 365569b337e77..394177a6516e8 100644 --- a/solution/2800-2899/2810.Faulty Keyboard/README.md +++ b/solution/2800-2899/2810.Faulty Keyboard/README.md @@ -158,6 +158,24 @@ function finalString(s: string): string { } ``` +### **Rust** + +```rust +impl Solution { + pub fn final_string(s: String) -> String { + let mut t = Vec::new(); + for c in s.chars() { + if c == 'i' { + t.reverse(); + } else { + t.push(c); + } + } + t.into_iter().collect() + } +} +``` + ### **...** ``` diff --git a/solution/2800-2899/2810.Faulty Keyboard/README_EN.md b/solution/2800-2899/2810.Faulty Keyboard/README_EN.md index ad0c0b656b7f4..d805c4efe4a07 100644 --- a/solution/2800-2899/2810.Faulty Keyboard/README_EN.md +++ b/solution/2800-2899/2810.Faulty Keyboard/README_EN.md @@ -140,6 +140,24 @@ function finalString(s: string): string { } ``` +### **Rust** + +```rust +impl Solution { + pub fn final_string(s: String) -> String { + let mut t = Vec::new(); + for c in s.chars() { + if c == 'i' { + t.reverse(); + } else { + t.push(c); + } + } + t.into_iter().collect() + } +} +``` + ### **...** ``` diff --git a/solution/2800-2899/2810.Faulty Keyboard/Solution.rs b/solution/2800-2899/2810.Faulty Keyboard/Solution.rs new file mode 100644 index 0000000000000..1f06b507f14e7 --- /dev/null +++ b/solution/2800-2899/2810.Faulty Keyboard/Solution.rs @@ -0,0 +1,13 @@ +impl Solution { + pub fn final_string(s: String) -> String { + let mut t = Vec::new(); + for c in s.chars() { + if c == 'i' { + t.reverse(); + } else { + t.push(c); + } + } + t.into_iter().collect() + } +} diff --git a/solution/2800-2899/2811.Check if it is Possible to Split Array/README.md b/solution/2800-2899/2811.Check if it is Possible to Split Array/README.md index 91e19f8f116fd..7ae40e99e3dcc 100644 --- a/solution/2800-2899/2811.Check if it is Possible to Split Array/README.md +++ b/solution/2800-2899/2811.Check if it is Possible to Split Array/README.md @@ -88,6 +88,14 @@ 时间复杂度 $O(n^3)$,空间复杂度 $O(n^2)$。其中 $n$ 是数组 $nums$ 的长度。 +**方法二:脑筋急转弯** + +不论如何操作,最终总会剩下一个 `length == 2` 的子数组,又因为元素数值不存在负数,所以随着分割操作的进行,子数组的长度和总和都会逐渐变小,其它 `length > 2` 子数组之和肯定要比该子数组之和更大,进而,我们只需要考虑,是否存在一个 `length == 2` 且总和大于等于 `m` 的子数组即可。 + +> 📢 注意,当 `nums.length <= 2` 时,无需进行操作。 + +时间复杂度 $O(n)$,空间复杂度 $O(1)$。 + ### **Python3** @@ -258,6 +266,40 @@ function canSplitArray(nums: number[], m: number): boolean { } ``` +```ts +function canSplitArray(nums: number[], m: number): boolean { + const n = nums.length; + if (n <= 2) { + return true; + } + for (let i = 1; i < n; i++) { + if (nums[i - 1] + nums[i] >= m) { + return true; + } + } + return false; +} +``` + +### **Rust** + +```rust +impl Solution { + pub fn can_split_array(nums: Vec, m: i32) -> bool { + let n = nums.len(); + if (n <= 2) { + return true; + } + for i in 1..n { + if nums[i - 1] + nums[i] >= m { + return true; + } + } + false + } +} +``` + ### **...** ``` diff --git a/solution/2800-2899/2811.Check if it is Possible to Split Array/README_EN.md b/solution/2800-2899/2811.Check if it is Possible to Split Array/README_EN.md index 4bb94b468c996..4b3532c2cb06e 100644 --- a/solution/2800-2899/2811.Check if it is Possible to Split Array/README_EN.md +++ b/solution/2800-2899/2811.Check if it is Possible to Split Array/README_EN.md @@ -217,6 +217,40 @@ function canSplitArray(nums: number[], m: number): boolean { } ``` +```ts +function canSplitArray(nums: number[], m: number): boolean { + const n = nums.length; + if (n <= 2) { + return true; + } + for (let i = 1; i < n; i++) { + if (nums[i - 1] + nums[i] >= m) { + return true; + } + } + return false; +} +``` + +### **Rust** + +```rust +impl Solution { + pub fn can_split_array(nums: Vec, m: i32) -> bool { + let n = nums.len(); + if (n <= 2) { + return true; + } + for i in 1..n { + if nums[i - 1] + nums[i] >= m { + return true; + } + } + false + } +} +``` + ### **...** ``` diff --git a/solution/2800-2899/2811.Check if it is Possible to Split Array/Solution.rs b/solution/2800-2899/2811.Check if it is Possible to Split Array/Solution.rs new file mode 100644 index 0000000000000..17fff0d17f5ce --- /dev/null +++ b/solution/2800-2899/2811.Check if it is Possible to Split Array/Solution.rs @@ -0,0 +1,14 @@ +impl Solution { + pub fn can_split_array(nums: Vec, m: i32) -> bool { + let n = nums.len(); + if (n <= 2) { + return true; + } + for i in 1..n { + if nums[i - 1] + nums[i] >= m { + return true; + } + } + false + } +} diff --git a/solution/2800-2899/2812.Find the Safest Path in a Grid/README.md b/solution/2800-2899/2812.Find the Safest Path in a Grid/README.md index 7d370a6b56317..4f8fd18173843 100644 --- a/solution/2800-2899/2812.Find the Safest Path in a Grid/README.md +++ b/solution/2800-2899/2812.Find the Safest Path in a Grid/README.md @@ -494,6 +494,132 @@ function maximumSafenessFactor(grid: number[][]): number { } ``` +```ts +function maximumSafenessFactor(grid: number[][]): number { + const n = grid.length; + const g = Array.from({ length: n }, () => new Array(n).fill(-1)); + const vis = Array.from({ length: n }, () => new Array(n).fill(false)); + let q: [number, number][] = []; + for (let i = 0; i < n; i++) { + for (let j = 0; j < n; j++) { + if (grid[i][j] === 1) { + q.push([i, j]); + } + } + } + let level = 0; + while (q.length) { + const t: [number, number][] = []; + for (const [x, y] of q) { + if (x < 0 || y < 0 || x === n || y === n || g[x][y] !== -1) { + continue; + } + g[x][y] = level; + t.push([x + 1, y]); + t.push([x - 1, y]); + t.push([x, y + 1]); + t.push([x, y - 1]); + } + q = t; + level++; + } + const dfs = (i: number, j: number, v: number) => { + if (i < 0 || j < 0 || i === n || j === n || vis[i][j] || g[i][j] <= v) { + return false; + } + vis[i][j] = true; + return ( + (i === n - 1 && j === n - 1) || + dfs(i + 1, j, v) || + dfs(i, j + 1, v) || + dfs(i - 1, j, v) || + dfs(i, j - 1, v) + ); + }; + + let left = 0; + let right = level; + while (left < right) { + vis.forEach(v => v.fill(false)); + const mid = (left + right) >>> 1; + if (dfs(0, 0, mid)) { + left = mid + 1; + } else { + right = mid; + } + } + return right; +} +``` + +### **Rust** + +```rust +use std::collections::VecDeque; +impl Solution { + fn dfs(i: usize, j: usize, v: i32, g: &Vec>, vis: &mut Vec>) -> bool { + if vis[i][j] || g[i][j] <= v { + return false; + } + vis[i][j] = true; + let n = g.len(); + i == n - 1 && j == n - 1 + || i != 0 && Self::dfs(i - 1, j, v, g, vis) + || i != n - 1 && Self::dfs(i + 1, j, v, g, vis) + || j != 0 && Self::dfs(i, j - 1, v, g, vis) + || j != n - 1 && Self::dfs(i, j + 1, v, g, vis) + } + + pub fn maximum_safeness_factor(grid: Vec>) -> i32 { + let n = grid.len(); + let mut g = vec![vec![-1; n]; n]; + let mut q = VecDeque::new(); + for i in 0..n { + for j in 0..n { + if grid[i][j] == 1 { + q.push_back((i, j)); + } + } + } + let mut level = 0; + while !q.is_empty() { + let m = q.len(); + for _ in 0..m { + let (i, j) = q.pop_front().unwrap(); + if g[i][j] != -1 { + continue; + } + g[i][j] = level; + if i != n - 1 { + q.push_back((i + 1, j)); + } + if i != 0 { + q.push_back((i - 1, j)); + } + if j != n - 1 { + q.push_back((i, j + 1)); + } + if j != 0 { + q.push_back((i, j - 1)); + } + } + level += 1; + } + let mut left = 0; + let mut right = level; + while left < right { + let mid = (left + right) >> 1; + if Self::dfs(0, 0, mid, &g, &mut vec![vec![false; n]; n]) { + left = mid + 1; + } else { + right = mid; + } + } + right + } +} +``` + ### **...** ``` diff --git a/solution/2800-2899/2812.Find the Safest Path in a Grid/README_EN.md b/solution/2800-2899/2812.Find the Safest Path in a Grid/README_EN.md index f88eeacc7b61d..235bf90853a3e 100644 --- a/solution/2800-2899/2812.Find the Safest Path in a Grid/README_EN.md +++ b/solution/2800-2899/2812.Find the Safest Path in a Grid/README_EN.md @@ -483,6 +483,132 @@ function maximumSafenessFactor(grid: number[][]): number { } ``` +```ts +function maximumSafenessFactor(grid: number[][]): number { + const n = grid.length; + const g = Array.from({ length: n }, () => new Array(n).fill(-1)); + const vis = Array.from({ length: n }, () => new Array(n).fill(false)); + let q: [number, number][] = []; + for (let i = 0; i < n; i++) { + for (let j = 0; j < n; j++) { + if (grid[i][j] === 1) { + q.push([i, j]); + } + } + } + let level = 0; + while (q.length) { + const t: [number, number][] = []; + for (const [x, y] of q) { + if (x < 0 || y < 0 || x === n || y === n || g[x][y] !== -1) { + continue; + } + g[x][y] = level; + t.push([x + 1, y]); + t.push([x - 1, y]); + t.push([x, y + 1]); + t.push([x, y - 1]); + } + q = t; + level++; + } + const dfs = (i: number, j: number, v: number) => { + if (i < 0 || j < 0 || i === n || j === n || vis[i][j] || g[i][j] <= v) { + return false; + } + vis[i][j] = true; + return ( + (i === n - 1 && j === n - 1) || + dfs(i + 1, j, v) || + dfs(i, j + 1, v) || + dfs(i - 1, j, v) || + dfs(i, j - 1, v) + ); + }; + + let left = 0; + let right = level; + while (left < right) { + vis.forEach(v => v.fill(false)); + const mid = (left + right) >>> 1; + if (dfs(0, 0, mid)) { + left = mid + 1; + } else { + right = mid; + } + } + return right; +} +``` + +### **Rust** + +```rust +use std::collections::VecDeque; +impl Solution { + fn dfs(i: usize, j: usize, v: i32, g: &Vec>, vis: &mut Vec>) -> bool { + if vis[i][j] || g[i][j] <= v { + return false; + } + vis[i][j] = true; + let n = g.len(); + i == n - 1 && j == n - 1 + || i != 0 && Self::dfs(i - 1, j, v, g, vis) + || i != n - 1 && Self::dfs(i + 1, j, v, g, vis) + || j != 0 && Self::dfs(i, j - 1, v, g, vis) + || j != n - 1 && Self::dfs(i, j + 1, v, g, vis) + } + + pub fn maximum_safeness_factor(grid: Vec>) -> i32 { + let n = grid.len(); + let mut g = vec![vec![-1; n]; n]; + let mut q = VecDeque::new(); + for i in 0..n { + for j in 0..n { + if grid[i][j] == 1 { + q.push_back((i, j)); + } + } + } + let mut level = 0; + while !q.is_empty() { + let m = q.len(); + for _ in 0..m { + let (i, j) = q.pop_front().unwrap(); + if g[i][j] != -1 { + continue; + } + g[i][j] = level; + if i != n - 1 { + q.push_back((i + 1, j)); + } + if i != 0 { + q.push_back((i - 1, j)); + } + if j != n - 1 { + q.push_back((i, j + 1)); + } + if j != 0 { + q.push_back((i, j - 1)); + } + } + level += 1; + } + let mut left = 0; + let mut right = level; + while left < right { + let mid = (left + right) >> 1; + if Self::dfs(0, 0, mid, &g, &mut vec![vec![false; n]; n]) { + left = mid + 1; + } else { + right = mid; + } + } + right + } +} +``` + ### **...** ``` diff --git a/solution/2800-2899/2812.Find the Safest Path in a Grid/Solution.rs b/solution/2800-2899/2812.Find the Safest Path in a Grid/Solution.rs new file mode 100644 index 0000000000000..b5fc358c5a402 --- /dev/null +++ b/solution/2800-2899/2812.Find the Safest Path in a Grid/Solution.rs @@ -0,0 +1,63 @@ +use std::collections::VecDeque; +impl Solution { + fn dfs(i: usize, j: usize, v: i32, g: &Vec>, vis: &mut Vec>) -> bool { + if vis[i][j] || g[i][j] <= v { + return false; + } + vis[i][j] = true; + let n = g.len(); + i == n - 1 && j == n - 1 + || i != 0 && Self::dfs(i - 1, j, v, g, vis) + || i != n - 1 && Self::dfs(i + 1, j, v, g, vis) + || j != 0 && Self::dfs(i, j - 1, v, g, vis) + || j != n - 1 && Self::dfs(i, j + 1, v, g, vis) + } + + pub fn maximum_safeness_factor(grid: Vec>) -> i32 { + let n = grid.len(); + let mut g = vec![vec![-1; n]; n]; + let mut q = VecDeque::new(); + for i in 0..n { + for j in 0..n { + if grid[i][j] == 1 { + q.push_back((i, j)); + } + } + } + let mut level = 0; + while !q.is_empty() { + let m = q.len(); + for _ in 0..m { + let (i, j) = q.pop_front().unwrap(); + if g[i][j] != -1 { + continue; + } + g[i][j] = level; + if i != n - 1 { + q.push_back((i + 1, j)); + } + if i != 0 { + q.push_back((i - 1, j)); + } + if j != n - 1 { + q.push_back((i, j + 1)); + } + if j != 0 { + q.push_back((i, j - 1)); + } + } + level += 1; + } + let mut left = 0; + let mut right = level; + while left < right { + let mid = (left + right) >> 1; + if Self::dfs(0, 0, mid, &g, &mut vec![vec![false; n]; n]) { + left = mid + 1; + } else { + right = mid; + } + } + right + } +}