package com.fishercoder.solutions;

public class _463 {

    public static class Solution1 {
        /**
         * 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
         * 1. we increment the count by 4 whenever we encounter an island
         * 2. also, we check in two directions: island's left and island's top, we only check these two directions,
         * see if this island has any island neighbors, if so, we'll deduct two from it.
         */
        public int islandPerimeter(int[][] grid) {
            int count = 0;
            for (int i = 0; i < grid.length; i++) {
                for (int j = 0; j < grid[0].length; j++) {
                    if (grid[i][j] == 1) {
                        count += 4;
                        if (i > 0 && grid[i - 1][j] == 1) {
                            count -= 2;
                        }
                        if (j > 0 && grid[i][j - 1] == 1) {
                            count -= 2;
                        }
                    }
                }
            }
            return count;
        }
    }
}

@@ -29,16 +29,70 @@
 tions;%0A%0A
+import java.util.LinkedList;%0Aimport java.util.Queue;%0A%0A
 public c
@@ -1210,11 +1210,1986 @@
  %7D%0A    %7D
+%0A%0A    public static class Solution2 %7B%0A        /**%0A         * My completely original solution on 10/4/2021:%0A         * Count the number of island neighbors that each island has, then reduce this number from four and add it to the result.%0A         */%0A        public int islandPerimeter(int%5B%5D%5B%5D grid) %7B%0A            int perimeter = 0;%0A            int m = grid.length;%0A            int n = grid%5B0%5D.length;%0A            boolean%5B%5D%5B%5D visited = new boolean%5Bm%5D%5Bn%5D;%0A            int%5B%5D directions = new int%5B%5D%7B0, 1, 0, -1, 0%7D;%0A            for (int i = 0; i %3C m; i++) %7B%0A                for (int j = 0; j %3C n; j++) %7B%0A                    if (grid%5Bi%5D%5Bj%5D == 1) %7B%0A                        Queue%3Cint%5B%5D%3E queue = new LinkedList%3C%3E();%0A                        queue.offer(new int%5B%5D%7Bi, j%7D);%0A                        while (!queue.isEmpty()) %7B%0A                            int%5B%5D curr = queue.poll();%0A                            if (!visited%5Bcurr%5B0%5D%5D%5Bcurr%5B1%5D%5D) %7B%0A                                visited%5Bcurr%5B0%5D%5D%5Bcurr%5B1%5D%5D = true;%0A                                int neighborCount = 0;%0A                                for (int k = 0; k %3C directions.length - 1; k++) %7B%0A                                    int newX = curr%5B0%5D + directions%5Bk%5D;%0A                                    int newY = curr%5B1%5D + directions%5Bk + 1%5D;%0A                                    if (newX %3E= 0 && newX %3C m && newY %3E= 0 && newY %3C n && grid%5BnewX%5D%5BnewY%5D == 1) %7B%0A                                        neighborCount++;%0A                                        if (!visited%5BnewX%5D%5BnewY%5D) %7B%0A                                            queue.offer(new int%5B%5D%7BnewX, newY%7D);%0A                                        %7D%0A                                    %7D%0A                                %7D%0A                                perimeter += 4 - neighborCount;%0A                            %7D%0A                        %7D%0A                        return perimeter;%0A                    %7D%0A                %7D%0A            %7D%0A            return perimeter;%0A        %7D%0A    %7D
 %0A%7D%0A

add 461