일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 31 |
Tags
- 월간
- 스탭퍼
- 3줄정리
- 뭐든
- 운동
- 30분
- 영어원서읽기
- 리얼 클래스
- 화상영어
- 개발자
- 영어공부
- 링피트
- realclass
- 잡생각
- 사이드
- 10분
- 프로젝트
- English
- Daily Challenge
- 쓰릴오브파이트
- 읽기
- Writing
- 괜찮음
- 매일
- 만화도
- leetcode
- 미드시청
- FIT XR
- Problem Solving
- 파비최
Archives
- Today
- Total
파비의 매일매일 공부기록
2023.10.25 Today's Challenge 본문
https://leetcode.com/problems/k-th-symbol-in-grammar/
K-th Symbol in Grammar - LeetCode
Can you solve this real interview question? K-th Symbol in Grammar - We build a table of n rows (1-indexed). We start by writing 0 in the 1st row. Now in every subsequent row, we look at the previous row and replace each occurrence of 0 with 01, and each o
leetcode.com
덮어놓고 배열을 만들다간 TL이 날 것 같은 문제.
솔루션을 보니 2진 트리로 만들어서 dfs 돌리는 방법 소개함.
더 놀라웠던건 수학적으로 증명하면 2줄로 마무리 됨 -_-;
class Solution:
def dfs(self, n, k, root):
if n == 1:
return root
total = 2 ** (n-1)
if k > (total / 2):
next_root = 1 if root == 0 else 0
return self.dfs(n-1, k-(total/2), next_root)
else:
next_root = 0 if root == 0 else 1
return self.dfs(n-1, k, next_root)
def kthGrammar(self, n: int, k: int) -> int:
return self.dfs(n, k, 0)
반응형
'Problem Solving > LeetCode' 카테고리의 다른 글
2023.10.27 Today's Challenge (0) | 2023.10.27 |
---|---|
2023.10.26 Today's Challenge (0) | 2023.10.26 |
2023.10.24 Today's Challenge (0) | 2023.10.24 |
2023.10.23 Today's Challenge (0) | 2023.10.23 |
2023.10.22 Today's Challenge (0) | 2023.10.22 |
Comments