-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindIslandPerimeter.js
More file actions
55 lines (51 loc) · 1.29 KB
/
Copy pathfindIslandPerimeter.js
File metadata and controls
55 lines (51 loc) · 1.29 KB
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
/**
* @param {number[][]} grid
* @return {number}
*/
var islandPerimeter = function(grid) {
const dir = [[-1,0],[0,1],[1,0],[0,-1]];
let i = 0;
let j = 0;
let ii, jj, cur, q;
let iEnd = grid.length - 1;
let jEnd = grid[i].length - 1;
let perimeter = 0;
//onBound helper method
let onBounds = (i, j) => i >= 0 && i <= iEnd && j >= 0 && j <= jEnd;
//find first plot of land
while(grid[i][j] == 0){
if(j == jEnd) {
i=i+1;
j = 0;
continue;
}
j++;
if(i > iEnd) return "can't find island"
};
//initialize plot of land q with first plot of land found
q = [[i,j]];
//bfs to count perimeter of island
while(q[0]){
i = q[0][0];
j = q[0][1];
for(d of dir) {
ii = i+d[0];
jj = j+d[1];
if(onBounds(ii, jj)){
cur = grid[ii][jj];
if(cur == -1) continue;
if(cur == 0 ) perimeter ++;
else if(cur == 1) {
q.push([ii,jj]);
grid[ii][jj] = -1;
};
}
else {
perimeter ++;
}
}
grid[i][j] = -1;
q.shift();
}
return perimeter;
};