| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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
- 잡생각
- FIT XR
- 30분
- 괜찮음
- 영어공부
- 월간
- 링피트
- 리얼 클래스
- 사이드
- leetcode
- 10분
- 만화도
- English
- 뭐든
- 영어원서읽기
- Writing
- 개발자
- 3줄정리
- 읽기
- 매일
- 파비최
- realclass
- 프로젝트
- Problem Solving
- 운동
- 스탭퍼
- Daily Challenge
- 쓰릴오브파이트
- 화상영어
- 미드시청
Archives
- Today
- Total
파비의 매일매일 공부기록
2023.02.27 Today's Challenge 본문
https://leetcode.com/problems/construct-quad-tree/
Construct Quad Tree - LeetCode
Can you solve this real interview question? Construct Quad Tree - Given a n * n matrix grid of 0's and 1's only. We want to represent the grid with a Quad-Tree. Return the root of the Quad-Tree representing the grid. Notice that you can assign the value of
leetcode.com
쿼드 트리 개념을 구현하는 문제.
class Solution:
def construct(self, grid: List[List[int]]) -> 'Node':
return self.helper(grid, 0, 0, len(grid))
def helper(self, grid, i, j, w):
if self.allSame(grid, i, j, w):
return Node(grid[i][j] == 1, True)
node = Node(True, False)
node.topLeft = self.helper(grid, i, j, w//2)
node.topRight = self.helper(grid, i, j+w // 2, w//2)
node.bottomLeft = self.helper(grid, i+w//2, j, w//2)
node.bottomRight = self.helper(grid, i+w//2, j+w//2, w//2)
return node
def allSame(self, grid, i, j, w):
for x in range(i, i+w):
for y in range(j, j+w):
if grid[x][y] != grid[i][j]:
return False
return True반응형
'Problem Solving > LeetCode' 카테고리의 다른 글
| 2023.03.01 Today's Challenge (0) | 2023.03.01 |
|---|---|
| 2023.02.28 Today's Challenge (0) | 2023.02.28 |
| 2023.02.26 Today's Challenge (0) | 2023.02.26 |
| 2023.02.25 Today's Challenge (0) | 2023.02.25 |
| 2023.02.24 Today's Challenge (0) | 2023.02.24 |
Comments