Skip to content

Commit 972b7b3

Browse files
committed
Add solution #1568
1 parent f19dca1 commit 972b7b3

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,373 LeetCode solutions in JavaScript
1+
# 1,374 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1197,6 +1197,7 @@
11971197
1563|[Stone Game V](./solutions/1563-stone-game-v.js)|Hard|
11981198
1566|[Detect Pattern of Length M Repeated K or More Times](./solutions/1566-detect-pattern-of-length-m-repeated-k-or-more-times.js)|Easy|
11991199
1567|[Maximum Length of Subarray With Positive Product](./solutions/1567-maximum-length-of-subarray-with-positive-product.js)|Medium|
1200+
1568|[Minimum Number of Days to Disconnect Island](./solutions/1568-minimum-number-of-days-to-disconnect-island.js)|Hard|
12001201
1576|[Replace All ?'s to Avoid Consecutive Repeating Characters](./solutions/1576-replace-all-s-to-avoid-consecutive-repeating-characters.js)|Medium|
12011202
1598|[Crawler Log Folder](./solutions/1598-crawler-log-folder.js)|Easy|
12021203
1657|[Determine if Two Strings Are Close](./solutions/1657-determine-if-two-strings-are-close.js)|Medium|
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* 1568. Minimum Number of Days to Disconnect Island
3+
* https://leetcode.com/problems/minimum-number-of-days-to-disconnect-island/
4+
* Difficulty: Hard
5+
*
6+
* You are given an m x n binary grid grid where 1 represents land and 0 represents water. An
7+
* island is a maximal 4-directionally (horizontal or vertical) connected group of 1's.
8+
*
9+
* The grid is said to be connected if we have exactly one island, otherwise is said disconnected.
10+
*
11+
* In one day, we are allowed to change any single land cell (1) into a water cell (0).
12+
*
13+
* Return the minimum number of days to disconnect the grid.
14+
*/
15+
16+
/**
17+
* @param {number[][]} grid
18+
* @return {number}
19+
*/
20+
var minDays = function(grid) {
21+
const rows = grid.length;
22+
const cols = grid[0].length;
23+
24+
if (countIslands() !== 1) return 0;
25+
26+
for (let r = 0; r < rows; r++) {
27+
for (let c = 0; c < cols; c++) {
28+
if (grid[r][c] === 1) {
29+
grid[r][c] = 0;
30+
if (countIslands() !== 1) return 1;
31+
grid[r][c] = 1;
32+
}
33+
}
34+
}
35+
36+
return 2;
37+
38+
function countIslands() {
39+
const visited = Array(rows).fill().map(() => Array(cols).fill(false));
40+
let islands = 0;
41+
42+
function dfs(row, col) {
43+
if (row < 0 || row >= rows || col < 0 || col >= cols
44+
|| visited[row][col] || grid[row][col] === 0) {
45+
return;
46+
}
47+
visited[row][col] = true;
48+
dfs(row + 1, col);
49+
dfs(row - 1, col);
50+
dfs(row, col + 1);
51+
dfs(row, col - 1);
52+
}
53+
54+
for (let r = 0; r < rows; r++) {
55+
for (let c = 0; c < cols; c++) {
56+
if (grid[r][c] === 1 && !visited[r][c]) {
57+
islands++;
58+
dfs(r, c);
59+
}
60+
}
61+
}
62+
return islands;
63+
}
64+
};

0 commit comments

Comments
 (0)