@@ -0,0 +1,2413 @@
+%22%22%22%0Ahttps://leetcode.com/problems/island-perimeter/description/%0A463. Island Perimeter%0A%0AYou 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 %22lakes%22 (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.%0A%0AExample:%0A%0A%5B%5B0,1,0,0%5D,%0A %5B1,1,1,0%5D,%0A %5B0,1,0,0%5D,%0A %5B1,1,0,0%5D%5D%0A%0AAnswer: 16%0AExplanation: The perimeter is the 16 yellow stripes in the image below:%0A%22%22%22%0Aclass Solution(object):%0A    def islandPerimeter(self, grid):%0A        %22%22%22%0A        :type grid: List%5BList%5Bint%5D%5D%0A        :rtype: int%0A        %22%22%22%0A        %22%22%22%0A        Steps:%0A            Say grid has m rows (length of grid) and n columns (length of each list inside the grid)%0A            - Loop through all cells within the grid%0A                - For cells == 1, find the count of 1s surrounding it by passing the cell index for left, right, up, down%0A                - Then do appropriate perimieter calculation%0A        %22%22%22%0A        # Height = m, number of rows (Iterate x on this)%0A        height = len(grid)%0A        # Length = n, number of columns (Iterate y on this)%0A        length = len(grid%5B0%5D)%0A        # Function inputs index of a unit and outputs value, if exists%0A        def surround_island(x,y):%0A            # For out-of-index cells, return 0%0A            if x %3C 0 or x %3E height - 1 or y %3C 0 or y %3E length - 1:%0A                return 0%0A            else:%0A                # Otherwise return the value%0A                return grid%5Bx%5D%5By%5D%0A        # perimeter to calculate perimeter%0A        perimeter = 0%0A        x = 0%0A        while x %3C height:%0A            y = 0%0A            while y %3C length:%0A                # Only check for cells that are equal to 1%0A                if grid%5Bx%5D%5By%5D == 1:%0A                    # Check its neighbors%0A                    counter = surround_island(x + 1, y) + surround_island(x, y + 1) + surround_island(x - 1, y) + surround_island(x, y - 1)%0A                    # Perimeter added by this cell%0A                    perimeter = perimeter + (4 - counter)%0A                y = y + 1%0A            x = x + 1%0A        return perimeter%0A

Create 463_island_perimeter.py