Advertisement
kosievdmerwe

Untitled

Oct 3rd, 2021
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.66 KB | None | 0 0
  1. class Solution:
  2.     def islandPerimeter(self, grid: List[List[int]]) -> int:
  3.         W = len(grid)
  4.         H = len(grid[0])
  5.        
  6.         def is_water(x: int, y: int) -> bool:
  7.             return not (
  8.                 0 <= x < W and
  9.                 0 <= y < H and
  10.                 grid[x][y] == 1
  11.             )
  12.        
  13.         ans = 0
  14.         for x in range(W):
  15.             for y in range(H):
  16.                 if is_water(x, y):
  17.                     continue
  18.                
  19.                 for dx, dy in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
  20.                     if is_water(x + dx, y + dy):
  21.                         ans += 1
  22.         return ans
  23.            
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement