-
Notifications
You must be signed in to change notification settings - Fork 157
Expand file tree
/
Copy path0463_island_perimeter_(easy).js
More file actions
89 lines (76 loc) · 1.99 KB
/
0463_island_perimeter_(easy).js
File metadata and controls
89 lines (76 loc) · 1.99 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/**
* 463. Island Perimeter
*
* https://leetcode.com/problems/island-perimeter/
*
* You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represents water.
* Grid cells are connected horizontally/vertically (not diagonally). The grid is completely surrounded by water,
* and there is exactly one island (i.e., one or more connected land cells). The island doesn't have "lakes"
* (water inside that isn't connected to the water around the island). One cell is a square with side length 1.
*
* The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.
*
* Example:
*
* [[0,1,0,0],
* [1,1,1,0],
* [0,1,0,0],
* [1,1,0,0]]
*
* Answer: 16
*/
/**
* Solution I
*
* @param {number[][]} grid
* @return {number}
*/
const islandPerimeter_I = (grid) => {
if (!grid || grid.length === 0) {
return 0;
}
const m = grid.length;
const n = grid[0].length;
let perimeter = 0;
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (grid[i][j] === 1) {
perimeter += i === 0 || grid[i - 1][j] === 0 ? 1 : 0;
perimeter += j === 0 || grid[i][j - 1] === 0 ? 1 : 0;
perimeter += j === n - 1 || grid[i][j + 1] === 0 ? 1 : 0;
perimeter += i === m - 1 || grid[i + 1][j] === 0 ? 1 : 0;
}
}
}
return perimeter;
};
/**
* Solution II
*
* @param {number[][]} grid
* @return {number}
*/
const islandPerimeter_II = (grid) => {
if (!grid || grid.length === 0) {
return 0;
}
const m = grid.length;
const n = grid[0].length;
let islands = 0;
let neighbours = 0;
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (grid[i][j] === 1) {
islands++;
if (i < m - 1 && grid[i + 1][j] === 1) {
neighbours++;
}
if (j < n - 1 && grid[i][j + 1] === 1) {
neighbours++;
}
}
}
}
return islands * 4 - neighbours * 2;
};
export { islandPerimeter_I, islandPerimeter_II };