일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- 링피트
- 화상영어
- 매일
- 10분
- 영어공부
- realclass
- 괜찮음
- 리얼 클래스
- FIT XR
- leetcode
- 잡생각
- 30분
- 파비최
- 미드시청
- 프로젝트
- 개발자
- 스탭퍼
- 만화도
- 읽기
- Problem Solving
- Writing
- 쓰릴오브파이트
- 3줄정리
- 월간
- 운동
- English
- 영어원서읽기
- 사이드
- 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