-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathMinimum Number of Days to Disconnect Island.js
65 lines (53 loc) · 1.73 KB
/
Minimum Number of Days to Disconnect Island.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
let directions = [[-1, 0], [1, 0], [0, -1], [0, 1]];
var minDays = function(grid) {
let colorNumber = 2;
let landCells = [];
function colorIsland(i, j) {
grid[i][j] = colorNumber;
for(let k = 0; k < directions.length; k++) {
let newI = i + directions[k][0];
let newJ = j + directions[k][1];
if(newI >= 0 && newJ >= 0 && newI < grid.length && newJ < grid[0].length && grid[newI][newJ] && grid[newI][newJ] != colorNumber) {
colorIsland(newI, newJ);
}
}
}
function addLandCells() {
for(let i = 0; i < grid.length; i++) {
for(let j = 0; j < grid[0].length; j++) {
if(grid[i][j]) {
landCells.push([i, j]);
}
}
}
}
function findAnotherIsland() {
for(let i = 0; i < grid.length; i++) {
for(let j = 0; j < grid[0].length; j++) {
if(grid[i][j] && grid[i][j] != colorNumber) {
return true;
}
}
}
return false;
}
addLandCells();
if(landCells.length < 2) {
return landCells.length;
}
colorIsland(landCells[0][0], landCells[0][1]);
if(findAnotherIsland()) {
return 0;
}
colorNumber++;
for(let i = 0; i < landCells.length; i++) {
grid[landCells[i][0]][landCells[i][1]] = 0;
colorIsland(landCells[(i + 1) % landCells.length][0], landCells[(i + 1) % landCells.length][1]);
if(findAnotherIsland()) {
return 1;
}
grid[landCells[i][0]][landCells[i][1]] = colorNumber;
colorNumber++;
}
return 2;
};