Skip to content

Commit 493eb33

Browse files
authored
feat: add solutions to lc problems: No.2810~2812 (#1476)
- No.2810.Faulty Keyboard - No.2811.Check if it is Possible to Split Array - No.2812.Find the Safest Path in a Grid
1 parent a4dacea commit 493eb33

File tree

9 files changed

+454
-0
lines changed

9 files changed

+454
-0
lines changed

solution/2800-2899/2810.Faulty Keyboard/README.md

+18
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,24 @@ function finalString(s: string): string {
158158
}
159159
```
160160

161+
### **Rust**
162+
163+
```rust
164+
impl Solution {
165+
pub fn final_string(s: String) -> String {
166+
let mut t = Vec::new();
167+
for c in s.chars() {
168+
if c == 'i' {
169+
t.reverse();
170+
} else {
171+
t.push(c);
172+
}
173+
}
174+
t.into_iter().collect()
175+
}
176+
}
177+
```
178+
161179
### **...**
162180

163181
```

solution/2800-2899/2810.Faulty Keyboard/README_EN.md

+18
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,24 @@ function finalString(s: string): string {
140140
}
141141
```
142142

143+
### **Rust**
144+
145+
```rust
146+
impl Solution {
147+
pub fn final_string(s: String) -> String {
148+
let mut t = Vec::new();
149+
for c in s.chars() {
150+
if c == 'i' {
151+
t.reverse();
152+
} else {
153+
t.push(c);
154+
}
155+
}
156+
t.into_iter().collect()
157+
}
158+
}
159+
```
160+
143161
### **...**
144162

145163
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
impl Solution {
2+
pub fn final_string(s: String) -> String {
3+
let mut t = Vec::new();
4+
for c in s.chars() {
5+
if c == 'i' {
6+
t.reverse();
7+
} else {
8+
t.push(c);
9+
}
10+
}
11+
t.into_iter().collect()
12+
}
13+
}

solution/2800-2899/2811.Check if it is Possible to Split Array/README.md

+42
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,14 @@
8888

8989
时间复杂度 $O(n^3)$,空间复杂度 $O(n^2)$。其中 $n$ 是数组 $nums$ 的长度。
9090

91+
**方法二:脑筋急转弯**
92+
93+
不论如何操作,最终总会剩下一个 `length == 2` 的子数组,又因为元素数值不存在负数,所以随着分割操作的进行,子数组的长度和总和都会逐渐变小,其它 `length > 2` 子数组之和肯定要比该子数组之和更大,进而,我们只需要考虑,是否存在一个 `length == 2` 且总和大于等于 `m` 的子数组即可。
94+
95+
> 📢 注意,当 `nums.length <= 2` 时,无需进行操作。
96+
97+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。
98+
9199
<!-- tabs:start -->
92100

93101
### **Python3**
@@ -258,6 +266,40 @@ function canSplitArray(nums: number[], m: number): boolean {
258266
}
259267
```
260268

269+
```ts
270+
function canSplitArray(nums: number[], m: number): boolean {
271+
const n = nums.length;
272+
if (n <= 2) {
273+
return true;
274+
}
275+
for (let i = 1; i < n; i++) {
276+
if (nums[i - 1] + nums[i] >= m) {
277+
return true;
278+
}
279+
}
280+
return false;
281+
}
282+
```
283+
284+
### **Rust**
285+
286+
```rust
287+
impl Solution {
288+
pub fn can_split_array(nums: Vec<i32>, m: i32) -> bool {
289+
let n = nums.len();
290+
if (n <= 2) {
291+
return true;
292+
}
293+
for i in 1..n {
294+
if nums[i - 1] + nums[i] >= m {
295+
return true;
296+
}
297+
}
298+
false
299+
}
300+
}
301+
```
302+
261303
### **...**
262304

263305
```

solution/2800-2899/2811.Check if it is Possible to Split Array/README_EN.md

+34
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,40 @@ function canSplitArray(nums: number[], m: number): boolean {
217217
}
218218
```
219219

220+
```ts
221+
function canSplitArray(nums: number[], m: number): boolean {
222+
const n = nums.length;
223+
if (n <= 2) {
224+
return true;
225+
}
226+
for (let i = 1; i < n; i++) {
227+
if (nums[i - 1] + nums[i] >= m) {
228+
return true;
229+
}
230+
}
231+
return false;
232+
}
233+
```
234+
235+
### **Rust**
236+
237+
```rust
238+
impl Solution {
239+
pub fn can_split_array(nums: Vec<i32>, m: i32) -> bool {
240+
let n = nums.len();
241+
if (n <= 2) {
242+
return true;
243+
}
244+
for i in 1..n {
245+
if nums[i - 1] + nums[i] >= m {
246+
return true;
247+
}
248+
}
249+
false
250+
}
251+
}
252+
```
253+
220254
### **...**
221255

222256
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
impl Solution {
2+
pub fn can_split_array(nums: Vec<i32>, m: i32) -> bool {
3+
let n = nums.len();
4+
if (n <= 2) {
5+
return true;
6+
}
7+
for i in 1..n {
8+
if nums[i - 1] + nums[i] >= m {
9+
return true;
10+
}
11+
}
12+
false
13+
}
14+
}

solution/2800-2899/2812.Find the Safest Path in a Grid/README.md

+126
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,132 @@ function maximumSafenessFactor(grid: number[][]): number {
494494
}
495495
```
496496

497+
```ts
498+
function maximumSafenessFactor(grid: number[][]): number {
499+
const n = grid.length;
500+
const g = Array.from({ length: n }, () => new Array(n).fill(-1));
501+
const vis = Array.from({ length: n }, () => new Array(n).fill(false));
502+
let q: [number, number][] = [];
503+
for (let i = 0; i < n; i++) {
504+
for (let j = 0; j < n; j++) {
505+
if (grid[i][j] === 1) {
506+
q.push([i, j]);
507+
}
508+
}
509+
}
510+
let level = 0;
511+
while (q.length) {
512+
const t: [number, number][] = [];
513+
for (const [x, y] of q) {
514+
if (x < 0 || y < 0 || x === n || y === n || g[x][y] !== -1) {
515+
continue;
516+
}
517+
g[x][y] = level;
518+
t.push([x + 1, y]);
519+
t.push([x - 1, y]);
520+
t.push([x, y + 1]);
521+
t.push([x, y - 1]);
522+
}
523+
q = t;
524+
level++;
525+
}
526+
const dfs = (i: number, j: number, v: number) => {
527+
if (i < 0 || j < 0 || i === n || j === n || vis[i][j] || g[i][j] <= v) {
528+
return false;
529+
}
530+
vis[i][j] = true;
531+
return (
532+
(i === n - 1 && j === n - 1) ||
533+
dfs(i + 1, j, v) ||
534+
dfs(i, j + 1, v) ||
535+
dfs(i - 1, j, v) ||
536+
dfs(i, j - 1, v)
537+
);
538+
};
539+
540+
let left = 0;
541+
let right = level;
542+
while (left < right) {
543+
vis.forEach(v => v.fill(false));
544+
const mid = (left + right) >>> 1;
545+
if (dfs(0, 0, mid)) {
546+
left = mid + 1;
547+
} else {
548+
right = mid;
549+
}
550+
}
551+
return right;
552+
}
553+
```
554+
555+
### **Rust**
556+
557+
```rust
558+
use std::collections::VecDeque;
559+
impl Solution {
560+
fn dfs(i: usize, j: usize, v: i32, g: &Vec<Vec<i32>>, vis: &mut Vec<Vec<bool>>) -> bool {
561+
if vis[i][j] || g[i][j] <= v {
562+
return false;
563+
}
564+
vis[i][j] = true;
565+
let n = g.len();
566+
i == n - 1 && j == n - 1
567+
|| i != 0 && Self::dfs(i - 1, j, v, g, vis)
568+
|| i != n - 1 && Self::dfs(i + 1, j, v, g, vis)
569+
|| j != 0 && Self::dfs(i, j - 1, v, g, vis)
570+
|| j != n - 1 && Self::dfs(i, j + 1, v, g, vis)
571+
}
572+
573+
pub fn maximum_safeness_factor(grid: Vec<Vec<i32>>) -> i32 {
574+
let n = grid.len();
575+
let mut g = vec![vec![-1; n]; n];
576+
let mut q = VecDeque::new();
577+
for i in 0..n {
578+
for j in 0..n {
579+
if grid[i][j] == 1 {
580+
q.push_back((i, j));
581+
}
582+
}
583+
}
584+
let mut level = 0;
585+
while !q.is_empty() {
586+
let m = q.len();
587+
for _ in 0..m {
588+
let (i, j) = q.pop_front().unwrap();
589+
if g[i][j] != -1 {
590+
continue;
591+
}
592+
g[i][j] = level;
593+
if i != n - 1 {
594+
q.push_back((i + 1, j));
595+
}
596+
if i != 0 {
597+
q.push_back((i - 1, j));
598+
}
599+
if j != n - 1 {
600+
q.push_back((i, j + 1));
601+
}
602+
if j != 0 {
603+
q.push_back((i, j - 1));
604+
}
605+
}
606+
level += 1;
607+
}
608+
let mut left = 0;
609+
let mut right = level;
610+
while left < right {
611+
let mid = (left + right) >> 1;
612+
if Self::dfs(0, 0, mid, &g, &mut vec![vec![false; n]; n]) {
613+
left = mid + 1;
614+
} else {
615+
right = mid;
616+
}
617+
}
618+
right
619+
}
620+
}
621+
```
622+
497623
### **...**
498624

499625
```

0 commit comments

Comments
 (0)