일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 3줄정리
- 영어공부
- 만화도
- FIT XR
- 미드시청
- Problem Solving
- 잡생각
- 쓰릴오브파이트
- 스탭퍼
- 매일
- Daily Challenge
- 리얼 클래스
- 월간
- 읽기
- 프로젝트
- 괜찮음
- 10분
- 화상영어
- 파비최
- Writing
- 운동
- 30분
- 사이드
- realclass
- 링피트
- leetcode
- 개발자
- English
- 뭐든
- 영어원서읽기
Archives
- Today
- Total
파비의 매일매일 공부기록
2023.08.17 Today's Challenge 본문
https://leetcode.com/problems/01-matrix/
01 Matrix - LeetCode
Can you solve this real interview question? 01 Matrix - Given an m x n binary matrix mat, return the distance of the nearest 0 for each cell. The distance between two adjacent cells is 1. Example 1: [https://assets.leetcode.com/uploads/2021/04/24/01-1-g
leetcode.com
생각보다 조금 어려웠던 문제.
BFS로 풀어야 함.
class Solution:
def updateMatrix(self, mat: List[List[int]]) -> List[List[int]]:
if not mat or not mat[0]:
return []
m, n = len(mat), len(mat[0])
qu = deque()
MAX_VALUE = m * n
for i in range(m):
for j in range(n):
if mat[i][j] == 0:
qu.append((i, j))
else:
mat[i][j] = MAX_VALUE
dirs = [(1, 0), (-1, 0), (0, 1), (0, -1)]
while qu:
row, col = qu.popleft()
for dr, dc in dirs:
r, c = row + dr, col + dc
if 0 <= r < m and 0 <= c < n and mat[r][c] > mat[row][col] + 1:
qu.append((r,c))
mat[r][c] = mat[row][col] + 1
return mat
반응형
'Problem Solving > LeetCode' 카테고리의 다른 글
2023.08.19 Today's Challenge (0) | 2023.08.19 |
---|---|
2023.08.18 Today's Challenge (0) | 2023.08.18 |
2023.08.16 Today's Challenge (0) | 2023.08.16 |
2023.08.15 Today's Challenge (0) | 2023.08.15 |
2023.08.14 Today's Challenge (0) | 2023.08.14 |
Comments