일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- Writing
- 괜찮음
- 개발자
- Problem Solving
- English
- 파비최
- 영어공부
- 30분
- 3줄정리
- 화상영어
- 만화도
- 리얼 클래스
- 운동
- 잡생각
- 영어원서읽기
- 10분
- 스탭퍼
- Daily Challenge
- 쓰릴오브파이트
- 매일
- realclass
- 읽기
- FIT XR
- 미드시청
- 링피트
- 뭐든
- 프로젝트
- 월간
- 사이드
- leetcode
Archives
- Today
- Total
파비의 매일매일 공부기록
2023.02.10 Today's Challenge 본문
https://leetcode.com/problems/as-far-from-land-as-possible/
As Far from Land as Possible - LeetCode
As Far from Land as Possible - Given an n x n grid containing only values 0 and 1, where 0 represents water and 1 represents land, find a water cell such that its distance to the nearest land cell is maximized, and return the distance. If no land or wa
leetcode.com
BFS로 풀면 되는 문제!
class Solution:
def maxDistance(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
q = deque()
for i in range(m):
for j in range(n):
if grid[i][j] == 1:
q.append((i, j, 0))
if not q or len(q) == m*n:
return -1
dirs = [(0, 1), (0, -1), (1, 0), (-1, 0)]
while q:
i, j, d = q.popleft()
for x, y in dirs:
ii, jj = i + x, j + y
if 0 <= ii < m and 0 <= jj < n and grid[ii][jj] == 0:
grid[ii][jj] = 1
q.append((ii, jj, d+1))
return d
반응형
'Problem Solving > LeetCode' 카테고리의 다른 글
2023.02.12 Today's Challenge (0) | 2023.02.12 |
---|---|
2023.02.11 Today's Challenge (0) | 2023.02.11 |
2023.02.09 Today's Challenge (0) | 2023.02.09 |
2023.02.08 Today's Challenge (0) | 2023.02.08 |
2023.02.07 Today's Challenge (0) | 2023.02.07 |
Comments