-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminimum-island.js
50 lines (40 loc) · 1.2 KB
/
minimum-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
const minimumIsland = (grid) => {
let minimum = Infinity;
const visited = new Set();
for (let r = 0; r < grid.length; r++){
for (let c = 0; c < grid[0].length; c++){
const size = exploreSize(grid,r,c,visited);
if (size > 0 && size < minimum){
minimum = size;
}
}
}
return minimum;
};
const exploreSize = (grid,r,c,visited) =>{
const rowInBound = 0 <= r && r < grid.length;
const columInBound = 0 <=c && c < grid[0].length;
if (!rowInBound || ! columInBound) return 0;
const pos = r + ',' + c;
if (visited.has(pos)) return 0;
visited.add(pos);
if (grid[r][c] === 'W') return 0;
let size = 1;
size += exploreSize(grid,r + 1,c,visited);
size += exploreSize(grid,r - 1,c,visited);
size += exploreSize(grid,r,c + 1,visited);
size += exploreSize(grid,r,c - 1,visited);
return size;
}
const grid = [
['W', 'L', 'W', 'W', 'W'],
['W', 'L', 'W', 'W', 'W'],
['W', 'W', 'W', 'L', 'W'],
['W', 'W', 'L', 'L', 'W'],
['L', 'W', 'W', 'L', 'L'],
['L', 'L', 'W', 'W', 'W'],
];
console.log(minimumIsland(grid)); //2
module.exports = {
minimumIsland,
};