Skip to content

Instantly share code, notes, and snippets.

@JyotinderSingh
Created July 7, 2020 13:48
Show Gist options
  • Save JyotinderSingh/2a3fe0c6d60789f9673c97cc7801926c to your computer and use it in GitHub Desktop.
Save JyotinderSingh/2a3fe0c6d60789f9673c97cc7801926c to your computer and use it in GitHub Desktop.
Island Perimeter (LeetCode) | Algorithm Explanation
class Solution {
public:
int islandPerimeter(vector<vector<int>>& grid) {
int perimeter = 0;
for(int i = 0; i < grid.size(); ++i) {
for(int j = 0; j < grid[0].size(); ++j) {
if(grid[i][j]) {
perimeter += (i == 0 || grid[i - 1][j] == 0) + (i == grid.size() - 1 || grid[i + 1][j] == 0) + (j == 0 || grid[i][j - 1] == 0) + (j == grid[0].size() - 1 || grid[i][j + 1] == 0);
}
}
}
return perimeter;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment