We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 3cfb9a5 commit 72747d5Copy full SHA for 72747d5
javascript/463-island-perimeter.js
@@ -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
18
19
20
21
22
23
+ return perimeter
24
+};
0 commit comments