Skip to content

Commit a049739

Browse files
committed
Add solution #463
1 parent 17ec001 commit a049739

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
448|[Find All Numbers Disappeared in an Array](./0448-find-all-numbers-disappeared-in-an-array.js)|Easy|
114114
451|[Sort Characters By Frequency](./0451-sort-characters-by-frequency.js)|Medium|
115115
459|[Repeated Substring Pattern](./0459-repeated-substring-pattern.js)|Easy|
116+
463|[Island Perimeter](./0463-island-perimeter.js)|Medium|
116117
476|[Number Complement](./0476-number-complement.js)|Easy|
117118
500|[Keyboard Row](./0500-keyboard-row.js)|Easy|
118119
506|[Relative Ranks](./0506-relative-ranks.js)|Easy|

solutions/0463-island-perimeter.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* 463. Island Perimeter
3+
* https://leetcode.com/problems/island-perimeter/
4+
* Difficulty: Medium
5+
*
6+
* You are given row x col grid representing a map where grid[i][j] = 1 represents land and
7+
* grid[i][j] = 0 represents water.
8+
*
9+
* Grid cells are connected horizontally/vertically (not diagonally). The grid is completely
10+
* surrounded by water, and there is exactly one island (i.e., one or more connected land cells).
11+
*
12+
* The island doesn't have "lakes", meaning the water inside isn't connected to the water around
13+
* the island. One cell is a square with side length 1. The grid is rectangular, width and height
14+
* don't exceed 100. Determine the perimeter of the island.
15+
*/
16+
17+
/**
18+
* @param {number[][]} grid
19+
* @return {number}
20+
*/
21+
var islandPerimeter = function(grid) {
22+
let island = 0;
23+
let adjacent = 0;
24+
25+
for (let i = 0; i < grid.length; i++) {
26+
for (let j = 0; j < grid[0].length; j++) {
27+
if (grid[i][j] === 1) {
28+
island++;
29+
if (i + 1 < grid.length && grid[i + 1][j] === 1) adjacent++;
30+
if (j + 1 < grid[0].length && grid[i][j + 1] === 1) adjacent++;
31+
}
32+
}
33+
}
34+
35+
return island * 4 - adjacent * 2;
36+
};

0 commit comments

Comments
 (0)