From b535d594481046b4d9b5c2672c495b6178a2139e Mon Sep 17 00:00:00 2001 From: Abhinandan <93651229+AE-Hertz@users.noreply.github.com> Date: Sun, 11 Aug 2024 13:23:43 +0530 Subject: [PATCH 1/4] feat: add Solution JS for lc no. 1568 --- .../Solution.js | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 solution/1500-1599/1568.Minimum Number of Days to Disconnect Island/Solution.js 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..b8a4ab3146714 --- /dev/null +++ b/solution/1500-1599/1568.Minimum Number of Days to Disconnect Island/Solution.js @@ -0,0 +1,48 @@ +/** + * @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; + +}; From 85fe1f3053bd035a3de2de69c70a9ac009c41caf Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Sun, 11 Aug 2024 16:17:49 +0800 Subject: [PATCH 2/4] Update Solution.js --- .../Solution.js | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) 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 index b8a4ab3146714..f41f97324fee1 100644 --- 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 @@ -2,16 +2,29 @@ * @param {number[][]} grid * @return {number} */ -var minDays = function(grid) { - const directions = [[0, 1], [1, 0], [0, -1], [-1, 0]]; +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]) { + 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); } } @@ -44,5 +57,4 @@ var minDays = function(grid) { } return 2; - }; From 789662e717af83b6f96ccd3db4f5c9ef333f79a6 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Sun, 11 Aug 2024 16:22:18 +0800 Subject: [PATCH 3/4] Update README.md --- .../README.md | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) 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; +}; +``` + From e5fa6c1ad233330a99789454573e2c9459ba7a99 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Sun, 11 Aug 2024 16:22:46 +0800 Subject: [PATCH 4/4] Update README_EN.md --- .../README_EN.md | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) 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; +}; +``` +