Created
July 15, 2020 05:03
-
-
Save munguial/ca3c35785541c663a55aff783c4c6b9d to your computer and use it in GitHub Desktop.
July - Day 7 - Island Perimeter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://leetcode.com/explore/challenge/card/july-leetcoding-challenge/544/week-1-july-1st-july-7th/3383/ | |
class Solution: | |
def islandPerimeter(self, grid: List[List[int]]) -> int: | |
def isInvalid(i: int, j: int) -> bool: | |
return i < 0 or i >= len(grid) or j < 0 or j >= len(grid[0]) | |
res = 0 | |
for i in range(len(grid)): | |
for j in range(len(grid[0])): | |
if grid[i][j] == 1: | |
for neighbor in [(-1, 0), (0, 1), (1, 0), (0, -1)]: | |
if isInvalid(i + neighbor[0], j + neighbor[1]) or \ | |
grid[i + neighbor[0]][j + neighbor[1]] == 0: | |
res += 1 | |
return res |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment