MCPcopy Create free account
hub / github.com/Hsinha11/Leetcode-solutions / islandPerimeter

Method islandPerimeter

463-Island-Perimeter/Solution.java:3–18  ·  view source on GitHub ↗
(int[][] grid)

Source from the content-addressed store, hash-verified

1// Time: 78 ms (85.12%) | Memory: 96.5 MB (51.90%) - LeetSync
2class Solution {
3 public int islandPerimeter(int[][] grid) {
4 int rows = grid.length;
5 int cols = grid[0].length;
6 int perimeter = 0;
7
8 for (int i = 0; i < rows; i++) {
9 for (int j = 0; j < cols; j++) {
10 if (grid[i][j] == 1) {
11 perimeter += 4;
12 if (i > 0 && grid[i - 1][j] == 1) perimeter -= 2;
13 if (j > 0 && grid[i][j - 1] == 1) perimeter -= 2;
14 }
15 }
16 }
17 return perimeter;
18 }
19}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected