Skip to content

feat: add ts solution to lc problem: No.1568 #3402

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 12, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,64 @@ func minDays(grid [][]int) int {
}
```

#### TypeScript

```ts
function minDays(grid: number[][]): number {
const [m, n] = [grid.length, grid[0].length];

const dfs = (i: number, j: number) => {
if (i < 0 || m <= i || j < 0 || n <= j || [0, 2].includes(grid[i][j])) return;

grid[i][j] = 2;
const dir = [-1, 0, 1, 0, -1];
for (let k = 0; k < 4; k++) {
const [y, x] = [i + dir[k], j + dir[k + 1]];
dfs(y, x);
}
};

const count = () => {
let c = 0;

for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (grid[i][j] === 1) {
dfs(i, j);
c++;
}
}
}

for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (grid[i][j] === 2) {
grid[i][j] = 1;
}
}
}

return c;
};

if (count() !== 1) return 0;

for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (grid[i][j] === 1) {
grid[i][j] = 0;

if (count() !== 1) return 1;

grid[i][j] = 1;
}
}
}

return 2;
}
```

#### JavaScript

```js
Expand All @@ -303,54 +361,42 @@ func minDays(grid [][]int) int {
* @return {number}
*/
var minDays = function (grid) {
const directions = [
[0, 1],
[1, 0],
[0, -1],
[-1, 0],
];
const rows = grid.length;
const cols = grid[0].length;

function dfs(x, y, visited) {
visited[x][y] = true;
for (let [dx, dy] of directions) {
const nx = x + dx,
ny = y + dy;
if (
nx >= 0 &&
ny >= 0 &&
nx < rows &&
ny < cols &&
grid[nx][ny] === 1 &&
!visited[nx][ny]
) {
dfs(nx, ny, visited);
}
const dirs = [-1, 0, 1, 0, -1];
const [m, n] = [grid.length, grid[0].length];

const dfs = (i, j, visited) => {
if (i < 0 || m <= i || j < 0 || n <= j || grid[i][j] === 0 || visited[i][j]) {
return;
}
}

function countIslands() {
let visited = Array.from({ length: rows }, () => Array(cols).fill(false));
let count = 0;
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
if (grid[i][j] === 1 && !visited[i][j]) {
count++;
dfs(i, j, visited);
visited[i][j] = true;
for (let d = 0; d < 4; d++) {
const [y, x] = [i + dirs[d], j + dirs[d + 1]];
dfs(y, x, visited);
}
};

const count = () => {
const vis = Array.from({ length: m }, () => Array(n).fill(false));
let c = 0;
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (grid[i][j] === 1 && !vis[i][j]) {
c++;
dfs(i, j, vis);
}
}
}
return count;
}
return c;
};

if (countIslands() !== 1) return 0;
if (count() !== 1) return 0;

for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (grid[i][j] === 1) {
grid[i][j] = 0;
if (countIslands() !== 1) return 1;
if (count() !== 1) return 1;
grid[i][j] = 1;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,64 @@ func minDays(grid [][]int) int {
}
```

#### TypeScript

```ts
function minDays(grid: number[][]): number {
const [m, n] = [grid.length, grid[0].length];

const dfs = (i: number, j: number) => {
if (i < 0 || m <= i || j < 0 || n <= j || [0, 2].includes(grid[i][j])) return;

grid[i][j] = 2;
const dir = [-1, 0, 1, 0, -1];
for (let k = 0; k < 4; k++) {
const [y, x] = [i + dir[k], j + dir[k + 1]];
dfs(y, x);
}
};

const count = () => {
let c = 0;

for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (grid[i][j] === 1) {
dfs(i, j);
c++;
}
}
}

for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (grid[i][j] === 2) {
grid[i][j] = 1;
}
}
}

return c;
};

if (count() !== 1) return 0;

for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (grid[i][j] === 1) {
grid[i][j] = 0;

if (count() !== 1) return 1;

grid[i][j] = 1;
}
}
}

return 2;
}
```

#### JavaScript

```js
Expand All @@ -294,54 +352,42 @@ func minDays(grid [][]int) int {
* @return {number}
*/
var minDays = function (grid) {
const directions = [
[0, 1],
[1, 0],
[0, -1],
[-1, 0],
];
const rows = grid.length;
const cols = grid[0].length;

function dfs(x, y, visited) {
visited[x][y] = true;
for (let [dx, dy] of directions) {
const nx = x + dx,
ny = y + dy;
if (
nx >= 0 &&
ny >= 0 &&
nx < rows &&
ny < cols &&
grid[nx][ny] === 1 &&
!visited[nx][ny]
) {
dfs(nx, ny, visited);
}
const dirs = [-1, 0, 1, 0, -1];
const [m, n] = [grid.length, grid[0].length];

const dfs = (i, j, visited) => {
if (i < 0 || m <= i || j < 0 || n <= j || grid[i][j] === 0 || visited[i][j]) {
return;
}
}

function countIslands() {
let visited = Array.from({ length: rows }, () => Array(cols).fill(false));
let count = 0;
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
if (grid[i][j] === 1 && !visited[i][j]) {
count++;
dfs(i, j, visited);
visited[i][j] = true;
for (let d = 0; d < 4; d++) {
const [y, x] = [i + dirs[d], j + dirs[d + 1]];
dfs(y, x, visited);
}
};

const count = () => {
const vis = Array.from({ length: m }, () => Array(n).fill(false));
let c = 0;
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (grid[i][j] === 1 && !vis[i][j]) {
c++;
dfs(i, j, vis);
}
}
}
return count;
}
return c;
};

if (countIslands() !== 1) return 0;
if (count() !== 1) return 0;

for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (grid[i][j] === 1) {
grid[i][j] = 0;
if (countIslands() !== 1) return 1;
if (count() !== 1) return 1;
grid[i][j] = 1;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,42 @@
* @return {number}
*/
var minDays = function (grid) {
const directions = [
[0, 1],
[1, 0],
[0, -1],
[-1, 0],
];
const rows = grid.length;
const cols = grid[0].length;
const dirs = [-1, 0, 1, 0, -1];
const [m, n] = [grid.length, grid[0].length];

function dfs(x, y, visited) {
visited[x][y] = true;
for (let [dx, dy] of directions) {
const nx = x + dx,
ny = y + dy;
if (
nx >= 0 &&
ny >= 0 &&
nx < rows &&
ny < cols &&
grid[nx][ny] === 1 &&
!visited[nx][ny]
) {
dfs(nx, ny, visited);
}
const dfs = (i, j, visited) => {
if (i < 0 || m <= i || j < 0 || n <= j || grid[i][j] === 0 || visited[i][j]) {
return;
}
}

function countIslands() {
let visited = Array.from({ length: rows }, () => Array(cols).fill(false));
let count = 0;
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
if (grid[i][j] === 1 && !visited[i][j]) {
count++;
dfs(i, j, visited);
visited[i][j] = true;
for (let d = 0; d < 4; d++) {
const [y, x] = [i + dirs[d], j + dirs[d + 1]];
dfs(y, x, visited);
}
};

const count = () => {
const vis = Array.from({ length: m }, () => Array(n).fill(false));
let c = 0;
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (grid[i][j] === 1 && !vis[i][j]) {
c++;
dfs(i, j, vis);
}
}
}
return count;
}
return c;
};

if (countIslands() !== 1) return 0;
if (count() !== 1) return 0;

for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (grid[i][j] === 1) {
grid[i][j] = 0;
if (countIslands() !== 1) return 1;
if (count() !== 1) return 1;
grid[i][j] = 1;
}
}
Expand Down
Loading
Loading