one line of code at a time
[leetcode] 463. Island Perimeter 파이썬 코드 본문
https://leetcode.com/problems/island-perimeter/
class Solution(object):
def islandPerimeter(self, grid):
m, n = len(grid), len(grid[0])
perimeter = 0
nei_dict = {0: 4, 1: 3, 2: 2, 3: 1, 4: 0}
for i in range(m):
for j in range(n):
if grid[i][j] == 1:
# check neighbors
nei = 0
directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]
for dr, dc in directions:
nr, nc = i + dr, j + dc
if 0 <= nr < m and 0 <= nc < n and grid[nr][nc] == 1:
nei += 1
perimeter += nei_dict[nei]
return perimeter
'leetcode' 카테고리의 다른 글
| [leetcode] 841. Keys and Rooms 파이썬 코드 (0) | 2024.09.24 |
|---|---|
| [leetcode] 17. Letter Combinations of a Phone Number 파이썬 코드 (0) | 2024.09.24 |
| [leetcode] 79. Word Search 파이썬 코드 (0) | 2024.09.24 |
| [leetcode] 802. Find Eventual Safe States 파이썬 코드 (2) | 2024.09.23 |
| [leetcode] 997. Find the Town Judge (0) | 2024.09.23 |