clueless coding // TODO: be smarter

LeetCode Island Perimeter: Java Solution



TL;DR Code Solution


public int islandPerimeter(int[][] grid) {

    int total = 0;
    for (int i = 0; i < grid.length; i++) {
        for (int j = 0; j < grid[0].length; j++) {
            if (grid[i][j] == 1) {
                total += 4;

                if (i < grid.length - 1 && grid[i + 1][j] == 1) {
                    total -= 2;
                }
                if (j < grid[0].length - 1 && grid[i][j + 1] == 1) {
                    total -= 2;
                }

            }
        }
    }
    return total;


}