diff --git a/solution/1500-1599/1568.Minimum Number of Days to Disconnect Island/README.md b/solution/1500-1599/1568.Minimum Number of Days to Disconnect Island/README.md index 37fc147aeb922..4d250f926d168 100644 --- a/solution/1500-1599/1568.Minimum Number of Days to Disconnect Island/README.md +++ b/solution/1500-1599/1568.Minimum Number of Days to Disconnect Island/README.md @@ -295,6 +295,71 @@ func minDays(grid [][]int) int { } ``` +#### JavaScript + +```js +/** + * @param {number[][]} grid + * @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); + } + } + } + + 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); + } + } + } + return count; + } + + if (countIslands() !== 1) return 0; + + for (let i = 0; i < rows; i++) { + for (let j = 0; j < cols; j++) { + if (grid[i][j] === 1) { + grid[i][j] = 0; + if (countIslands() !== 1) return 1; + grid[i][j] = 1; + } + } + } + + return 2; +}; +``` + diff --git a/solution/1500-1599/1568.Minimum Number of Days to Disconnect Island/README_EN.md b/solution/1500-1599/1568.Minimum Number of Days to Disconnect Island/README_EN.md index 76e9a2206e446..1770738b97e1a 100644 --- a/solution/1500-1599/1568.Minimum Number of Days to Disconnect Island/README_EN.md +++ b/solution/1500-1599/1568.Minimum Number of Days to Disconnect Island/README_EN.md @@ -286,6 +286,71 @@ func minDays(grid [][]int) int { } ``` +#### JavaScript + +```js +/** + * @param {number[][]} grid + * @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); + } + } + } + + 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); + } + } + } + return count; + } + + if (countIslands() !== 1) return 0; + + for (let i = 0; i < rows; i++) { + for (let j = 0; j < cols; j++) { + if (grid[i][j] === 1) { + grid[i][j] = 0; + if (countIslands() !== 1) return 1; + grid[i][j] = 1; + } + } + } + + return 2; +}; +``` + diff --git a/solution/1500-1599/1568.Minimum Number of Days to Disconnect Island/Solution.js b/solution/1500-1599/1568.Minimum Number of Days to Disconnect Island/Solution.js new file mode 100644 index 0000000000000..f41f97324fee1 --- /dev/null +++ b/solution/1500-1599/1568.Minimum Number of Days to Disconnect Island/Solution.js @@ -0,0 +1,60 @@ +/** + * @param {number[][]} grid + * @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); + } + } + } + + 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); + } + } + } + return count; + } + + if (countIslands() !== 1) return 0; + + for (let i = 0; i < rows; i++) { + for (let j = 0; j < cols; j++) { + if (grid[i][j] === 1) { + grid[i][j] = 0; + if (countIslands() !== 1) return 1; + grid[i][j] = 1; + } + } + } + + return 2; +};