Skip to content

Commit 0380cac

Browse files
committed
feat: add solutions to lc problem: No.0827
No.0827.Making A Large Island
1 parent 2fad043 commit 0380cac

File tree

4 files changed

+449
-0
lines changed

4 files changed

+449
-0
lines changed

solution/0800-0899/0827.Making A Large Island/README.md

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,159 @@ func max(a, b int) int {
568568
}
569569
```
570570

571+
### **TypeScript**
572+
573+
```ts
574+
function largestIsland(grid: number[][]): number {
575+
const n = grid.length;
576+
const vis = Array.from({ length: n }, () => new Array(n).fill(false));
577+
const group = Array.from({ length: n }, () => new Array(n).fill(0));
578+
const dfs = (i: number, j: number, paths: [number, number][]) => {
579+
if (
580+
i < 0 ||
581+
j < 0 ||
582+
i === n ||
583+
j === n ||
584+
vis[i][j] ||
585+
grid[i][j] !== 1
586+
) {
587+
return;
588+
}
589+
vis[i][j] = true;
590+
paths.push([i, j]);
591+
dfs(i + 1, j, paths);
592+
dfs(i, j + 1, paths);
593+
dfs(i - 1, j, paths);
594+
dfs(i, j - 1, paths);
595+
};
596+
let count = 1;
597+
for (let i = 0; i < n; i++) {
598+
for (let j = 0; j < n; j++) {
599+
const paths: [number, number][] = [];
600+
dfs(i, j, paths);
601+
if (paths.length !== 0) {
602+
for (const [x, y] of paths) {
603+
group[x][y] = count;
604+
grid[x][y] = paths.length;
605+
}
606+
count++;
607+
}
608+
}
609+
}
610+
611+
let res = 0;
612+
for (let i = 0; i < n; i++) {
613+
for (let j = 0; j < n; j++) {
614+
let sum = grid[i][j];
615+
if (grid[i][j] === 0) {
616+
sum++;
617+
const set = new Set();
618+
if (i !== 0) {
619+
sum += grid[i - 1][j];
620+
set.add(group[i - 1][j]);
621+
}
622+
if (i !== n - 1 && !set.has(group[i + 1][j])) {
623+
sum += grid[i + 1][j];
624+
set.add(group[i + 1][j]);
625+
}
626+
if (j !== 0 && !set.has(group[i][j - 1])) {
627+
sum += grid[i][j - 1];
628+
set.add(group[i][j - 1]);
629+
}
630+
if (j !== n - 1 && !set.has(group[i][j + 1])) {
631+
sum += grid[i][j + 1];
632+
}
633+
}
634+
res = Math.max(res, sum);
635+
}
636+
}
637+
return res;
638+
}
639+
```
640+
641+
### **Rust**
642+
643+
```rust
644+
use std::collections::HashSet;
645+
impl Solution {
646+
fn dfs(
647+
i: usize,
648+
j: usize,
649+
grid: &Vec<Vec<i32>>,
650+
paths: &mut Vec<(usize, usize)>,
651+
vis: &mut Vec<Vec<bool>>,
652+
) {
653+
let n = vis.len();
654+
if vis[i][j] || grid[i][j] != 1 {
655+
return;
656+
}
657+
paths.push((i, j));
658+
vis[i][j] = true;
659+
if i != 0 {
660+
Self::dfs(i - 1, j, grid, paths, vis);
661+
}
662+
if j != 0 {
663+
Self::dfs(i, j - 1, grid, paths, vis);
664+
}
665+
if i != n - 1 {
666+
Self::dfs(i + 1, j, grid, paths, vis);
667+
}
668+
if j != n - 1 {
669+
Self::dfs(i, j + 1, grid, paths, vis);
670+
}
671+
}
672+
673+
pub fn largest_island(mut grid: Vec<Vec<i32>>) -> i32 {
674+
let n = grid.len();
675+
let mut vis = vec![vec![false; n]; n];
676+
let mut group = vec![vec![0; n]; n];
677+
let mut count = 1;
678+
for i in 0..n {
679+
for j in 0..n {
680+
let mut paths: Vec<(usize, usize)> = Vec::new();
681+
Self::dfs(i, j, &grid, &mut paths, &mut vis);
682+
let m = paths.len() as i32;
683+
if m != 0 {
684+
for (x, y) in paths {
685+
grid[x][y] = m;
686+
group[x][y] = count;
687+
}
688+
count += 1;
689+
}
690+
}
691+
}
692+
let mut res = 0;
693+
for i in 0..n {
694+
for j in 0..n {
695+
let mut sum = grid[i][j];
696+
if grid[i][j] == 0 {
697+
sum += 1;
698+
let mut set = HashSet::new();
699+
if i != 0 {
700+
sum += grid[i - 1][j];
701+
set.insert(group[i - 1][j]);
702+
}
703+
if j != 0 && !set.contains(&group[i][j - 1]) {
704+
sum += grid[i][j - 1];
705+
set.insert(group[i][j - 1]);
706+
}
707+
if i != n - 1 && !set.contains(&group[i + 1][j]) {
708+
sum += grid[i + 1][j];
709+
set.insert(group[i + 1][j]);
710+
}
711+
if j != n - 1 && !set.contains(&group[i][j + 1]) {
712+
sum += grid[i][j + 1];
713+
set.insert(group[i][j + 1]);
714+
}
715+
}
716+
res = res.max(sum);
717+
}
718+
}
719+
res
720+
}
721+
}
722+
```
723+
571724
### **...**
572725

573726
```

solution/0800-0899/0827.Making A Large Island/README_EN.md

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,159 @@ func max(a, b int) int {
520520
}
521521
```
522522

523+
### **TypeScript**
524+
525+
```ts
526+
function largestIsland(grid: number[][]): number {
527+
const n = grid.length;
528+
const vis = Array.from({ length: n }, () => new Array(n).fill(false));
529+
const group = Array.from({ length: n }, () => new Array(n).fill(0));
530+
const dfs = (i: number, j: number, paths: [number, number][]) => {
531+
if (
532+
i < 0 ||
533+
j < 0 ||
534+
i === n ||
535+
j === n ||
536+
vis[i][j] ||
537+
grid[i][j] !== 1
538+
) {
539+
return;
540+
}
541+
vis[i][j] = true;
542+
paths.push([i, j]);
543+
dfs(i + 1, j, paths);
544+
dfs(i, j + 1, paths);
545+
dfs(i - 1, j, paths);
546+
dfs(i, j - 1, paths);
547+
};
548+
let count = 1;
549+
for (let i = 0; i < n; i++) {
550+
for (let j = 0; j < n; j++) {
551+
const paths: [number, number][] = [];
552+
dfs(i, j, paths);
553+
if (paths.length !== 0) {
554+
for (const [x, y] of paths) {
555+
group[x][y] = count;
556+
grid[x][y] = paths.length;
557+
}
558+
count++;
559+
}
560+
}
561+
}
562+
563+
let res = 0;
564+
for (let i = 0; i < n; i++) {
565+
for (let j = 0; j < n; j++) {
566+
let sum = grid[i][j];
567+
if (grid[i][j] === 0) {
568+
sum++;
569+
const set = new Set();
570+
if (i !== 0) {
571+
sum += grid[i - 1][j];
572+
set.add(group[i - 1][j]);
573+
}
574+
if (i !== n - 1 && !set.has(group[i + 1][j])) {
575+
sum += grid[i + 1][j];
576+
set.add(group[i + 1][j]);
577+
}
578+
if (j !== 0 && !set.has(group[i][j - 1])) {
579+
sum += grid[i][j - 1];
580+
set.add(group[i][j - 1]);
581+
}
582+
if (j !== n - 1 && !set.has(group[i][j + 1])) {
583+
sum += grid[i][j + 1];
584+
}
585+
}
586+
res = Math.max(res, sum);
587+
}
588+
}
589+
return res;
590+
}
591+
```
592+
593+
### **Rust**
594+
595+
```rust
596+
use std::collections::HashSet;
597+
impl Solution {
598+
fn dfs(
599+
i: usize,
600+
j: usize,
601+
grid: &Vec<Vec<i32>>,
602+
paths: &mut Vec<(usize, usize)>,
603+
vis: &mut Vec<Vec<bool>>,
604+
) {
605+
let n = vis.len();
606+
if vis[i][j] || grid[i][j] != 1 {
607+
return;
608+
}
609+
paths.push((i, j));
610+
vis[i][j] = true;
611+
if i != 0 {
612+
Self::dfs(i - 1, j, grid, paths, vis);
613+
}
614+
if j != 0 {
615+
Self::dfs(i, j - 1, grid, paths, vis);
616+
}
617+
if i != n - 1 {
618+
Self::dfs(i + 1, j, grid, paths, vis);
619+
}
620+
if j != n - 1 {
621+
Self::dfs(i, j + 1, grid, paths, vis);
622+
}
623+
}
624+
625+
pub fn largest_island(mut grid: Vec<Vec<i32>>) -> i32 {
626+
let n = grid.len();
627+
let mut vis = vec![vec![false; n]; n];
628+
let mut group = vec![vec![0; n]; n];
629+
let mut count = 1;
630+
for i in 0..n {
631+
for j in 0..n {
632+
let mut paths: Vec<(usize, usize)> = Vec::new();
633+
Self::dfs(i, j, &grid, &mut paths, &mut vis);
634+
let m = paths.len() as i32;
635+
if m != 0 {
636+
for (x, y) in paths {
637+
grid[x][y] = m;
638+
group[x][y] = count;
639+
}
640+
count += 1;
641+
}
642+
}
643+
}
644+
let mut res = 0;
645+
for i in 0..n {
646+
for j in 0..n {
647+
let mut sum = grid[i][j];
648+
if grid[i][j] == 0 {
649+
sum += 1;
650+
let mut set = HashSet::new();
651+
if i != 0 {
652+
sum += grid[i - 1][j];
653+
set.insert(group[i - 1][j]);
654+
}
655+
if j != 0 && !set.contains(&group[i][j - 1]) {
656+
sum += grid[i][j - 1];
657+
set.insert(group[i][j - 1]);
658+
}
659+
if i != n - 1 && !set.contains(&group[i + 1][j]) {
660+
sum += grid[i + 1][j];
661+
set.insert(group[i + 1][j]);
662+
}
663+
if j != n - 1 && !set.contains(&group[i][j + 1]) {
664+
sum += grid[i][j + 1];
665+
set.insert(group[i][j + 1]);
666+
}
667+
}
668+
res = res.max(sum);
669+
}
670+
}
671+
res
672+
}
673+
}
674+
```
675+
523676
### **...**
524677

525678
```
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
use std::collections::HashSet;
2+
impl Solution {
3+
fn dfs(
4+
i: usize,
5+
j: usize,
6+
grid: &Vec<Vec<i32>>,
7+
paths: &mut Vec<(usize, usize)>,
8+
vis: &mut Vec<Vec<bool>>,
9+
) {
10+
let n = vis.len();
11+
if vis[i][j] || grid[i][j] != 1 {
12+
return;
13+
}
14+
paths.push((i, j));
15+
vis[i][j] = true;
16+
if i != 0 {
17+
Self::dfs(i - 1, j, grid, paths, vis);
18+
}
19+
if j != 0 {
20+
Self::dfs(i, j - 1, grid, paths, vis);
21+
}
22+
if i != n - 1 {
23+
Self::dfs(i + 1, j, grid, paths, vis);
24+
}
25+
if j != n - 1 {
26+
Self::dfs(i, j + 1, grid, paths, vis);
27+
}
28+
}
29+
30+
pub fn largest_island(mut grid: Vec<Vec<i32>>) -> i32 {
31+
let n = grid.len();
32+
let mut vis = vec![vec![false; n]; n];
33+
let mut group = vec![vec![0; n]; n];
34+
let mut count = 1;
35+
for i in 0..n {
36+
for j in 0..n {
37+
let mut paths: Vec<(usize, usize)> = Vec::new();
38+
Self::dfs(i, j, &grid, &mut paths, &mut vis);
39+
let m = paths.len() as i32;
40+
if m != 0 {
41+
for (x, y) in paths {
42+
grid[x][y] = m;
43+
group[x][y] = count;
44+
}
45+
count += 1;
46+
}
47+
}
48+
}
49+
let mut res = 0;
50+
for i in 0..n {
51+
for j in 0..n {
52+
let mut sum = grid[i][j];
53+
if grid[i][j] == 0 {
54+
sum += 1;
55+
let mut set = HashSet::new();
56+
if i != 0 {
57+
sum += grid[i - 1][j];
58+
set.insert(group[i - 1][j]);
59+
}
60+
if j != 0 && !set.contains(&group[i][j - 1]) {
61+
sum += grid[i][j - 1];
62+
set.insert(group[i][j - 1]);
63+
}
64+
if i != n - 1 && !set.contains(&group[i + 1][j]) {
65+
sum += grid[i + 1][j];
66+
set.insert(group[i + 1][j]);
67+
}
68+
if j != n - 1 && !set.contains(&group[i][j + 1]) {
69+
sum += grid[i][j + 1];
70+
set.insert(group[i][j + 1]);
71+
}
72+
}
73+
res = res.max(sum);
74+
}
75+
}
76+
res
77+
}
78+
}

0 commit comments

Comments
 (0)