Island Perimeter
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represents water.
Grid cells are connected horizontally/vertically (not diagonally). The grid is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).
The island doesn't have "lakes" (water inside that isn't connected to the water around the island). One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.
Example:
Input: [[0,1,0,0], [1,1,1,0], [0,1,0,0], [1,1,0,0]] Output: 16 Explanation: The perimeter is the 16 yellow stripes in the image below:
class Solution {
public int islandPerimeter(int[][] grid) {
int lands = 0, neighbours = 0;
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[i].length; j++) {
if (grid[i][j] == 1) {
lands++;
if (i < grid.length - 1 && grid[i + 1][j] == 1) neighbours++; //step 2
if (j < grid[i].length - 1 && grid[i][j + 1] == 1) neighbours++; //step3
}
}
}
return lands * 4 - neighbours * 2;
}
}
Here, we are going to solve using normal matrix traversal.
1) If 1 is found, then that cell must be a land and increase land count.
2) Step 2 in code is to check for any lands in down (i.e., neighbours)
3) Step 3 in code is to check for any lands in right(i.e., neighbours)
4) Finally return lands*4 - neighbours*2. This is pattern hidden in image in problem description. since every adjacent lands made two sides disappeared (Just compare with above image).
Example
Consider there are two lands, then total perimeter is 8. Now assume if both are adjacent, then total perimeter is 6. This is reason behind for making the return result formula.
Related Post:
Click here for April month challenges with solution and explanation
😍..Happy Coding...😍
Click Follow button to receive latest updates from us instantly.
Please let us know about your views in comment section.
Help Others, Please Share..!!!!
Nice and concise, My mistake i started solving using brute force. I need to think in terms of generalizing the prob statement. Thanks!!
ReplyDeleteThank You Amrendra Yadav for encouraging us by your wonderful feedback. This will motive us to achieve more. Whatever problem, first check is there any pattern hidden in problem, so that you can complete it in more concise solution. Please do follow us to receive latest updates instantly and keep supporting. Happy Coding...!!!
DeleteCan you help me understand this `The island doesn't have "lakes"` this is something I haven't go it yet. what does this means
ReplyDeletebecause with the given input
[[1,0,0,0],[0,0,0,0],[0,0,0,0],[1,1,1,1]]
the expected result does not matched the output
Thanks for posting your doubt. The island doesn't have lakes means that if you are forming island but inside there is one cell with water (which means island having lakes). According to problem island should be completely connected and surrounded by water, also within island there should be no water cells. Please do follow us to receive latest updates from us instantly.
Delete