clueless coding // TODO: be smarter

LeetCode Island Perimeter: CPP Solution



TL;DR Code Solution


int islandPerimeter(vector<vector<int>>& grid) {

    int total = 0;        

    for (int i = 0; i < grid.size(); i++) {
        for (int j = 0; j < grid[i].size(); j++) {
            if (grid[i][j]) {
                total += 4;
                if (i > 0 && grid[i-1][j]) total -= 2;
                if (j > 0 && grid[i][j-1]) total -= 2;
            }
        }
    }

    return total;

}