Skip to content

Commit 72747d5

Browse files
authored
Create 463-island-perimeter.js
solved island-perimeter in JS.
1 parent 3cfb9a5 commit 72747d5

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

javascript/463-island-perimeter.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// problem link https://leetcode.com/problems/island-perimeter
2+
// time complexity. it looks O(n^2) but it's actually constant time O(1). Because the input widht and height won't exeed 100 as stated in the problem.
3+
4+
var islandPerimeter = function(grid) {
5+
const row = grid.length;
6+
const column = grid[0].length;
7+
let perimeter = 0;
8+
for(let i = 0; i < row; i++) {
9+
for(let j = 0; j< column; j++) {
10+
if(grid[i][j] == 1) {
11+
perimeter += 4;
12+
13+
if(i > 0 && grid[i - 1][j] == 1) {
14+
perimeter -= 2;
15+
}
16+
if(j > 0 && grid[i][j - 1] == 1) {
17+
perimeter -= 2;
18+
}
19+
}
20+
}
21+
}
22+
23+
return perimeter
24+
};

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy