|
1 | 1 | package com.fishercoder.solutions;
|
2 | 2 | /**
|
3 |
| - * You are given a map in form of a two-dimensional integer grid |
4 |
| - * where 1 represents land and 0 represents water. |
| 3 | + * 463. Island Perimeter |
| 4 | + * |
| 5 | + * You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represents water. |
5 | 6 | * Grid cells are connected horizontally/vertically (not diagonally).
|
6 |
| - * The grid is completely surrounded by water, |
7 |
| - * and there is exactly one island (i.e., one or more connected land cells). |
| 7 | + * The grid is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells). |
8 | 8 | * The island doesn't have "lakes" (water inside that isn't connected to the water around the island).
|
9 | 9 | * One cell is a square with side length 1.
|
10 | 10 | * The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.
|
|
18 | 18 |
|
19 | 19 | Answer: 16
|
20 | 20 | Explanation: The perimeter is the 16 yellow stripes in the image below:
|
21 |
| -
|
22 | 21 | */
|
23 | 22 | public class _463 {
|
24 |
| - /**Inspired by this post: https://discuss.leetcode.com/topic/68983/java-9-line-solution-add-4-for-each-land-and-remove-2-for-each-internal-edge |
25 |
| - * 1. we increment the count by 4 whenever we encounter an island |
26 |
| - * 2. also, we check in two directions: island's left and island's top, we only check these two directions, |
27 |
| - * see if this island has any island neighbors, if so, we'll deduct two from it.*/ |
28 |
| - public int islandPerimeter(int[][] grid) { |
29 |
| - int count = 0; |
30 |
| - for (int i = 0; i < grid.length; i++) { |
31 |
| - for (int j = 0; j < grid[0].length; j++) { |
32 |
| - if (grid[i][j] == 1) { |
33 |
| - count += 4; |
34 |
| - if (i > 0 && grid[i - 1][j] == 1) { |
35 |
| - count -= 2; |
36 |
| - } |
37 |
| - if (j > 0 && grid[i][j - 1] == 1) { |
38 |
| - count -= 2; |
| 23 | + |
| 24 | + public static class Solution1 { |
| 25 | + /** |
| 26 | + * Inspired by this post: https://discuss.leetcode.com/topic/68983/java-9-line-solution-add-4-for-each-land-and-remove-2-for-each-internal-edge |
| 27 | + * 1. we increment the count by 4 whenever we encounter an island |
| 28 | + * 2. also, we check in two directions: island's left and island's top, we only check these two directions, |
| 29 | + * see if this island has any island neighbors, if so, we'll deduct two from it. |
| 30 | + */ |
| 31 | + public int islandPerimeter(int[][] grid) { |
| 32 | + int count = 0; |
| 33 | + for (int i = 0; i < grid.length; i++) { |
| 34 | + for (int j = 0; j < grid[0].length; j++) { |
| 35 | + if (grid[i][j] == 1) { |
| 36 | + count += 4; |
| 37 | + if (i > 0 && grid[i - 1][j] == 1) { |
| 38 | + count -= 2; |
| 39 | + } |
| 40 | + if (j > 0 && grid[i][j - 1] == 1) { |
| 41 | + count -= 2; |
| 42 | + } |
39 | 43 | }
|
40 | 44 | }
|
41 | 45 | }
|
| 46 | + return count; |
42 | 47 | }
|
43 |
| - return count; |
44 | 48 | }
|
45 | 49 | }
|
0 commit comments