forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
259 additions
and
0 deletions.
There are no files selected for viewing
98 changes: 98 additions & 0 deletions
98
construct-binary-tree-from-preorder-and-inorder-traversal/haklee.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
"""TC: O(n), SC: O(n) | ||
์์ด๋์ด: | ||
- preorder ํธ๋ฆฌ๊ฐ ์ฃผ์ด์ ธ ์๋ค๋ฉด ๋ค์๊ณผ ๊ฐ์ด ๋ถํ ํ ์ ์๋ค. | ||
- [root๊ฐ, [...left], [...right]] | ||
- ์์ left, right๋ preorder ํธ๋ฆฌ์ ๊ฐ์ ๋ฐฉ์์ผ๋ก ๊ตฌ์ฑ๋๋ค. | ||
- inorder ํธ๋ฆฌ๊ฐ ์ฃผ์ด์ ธ ์๋ค๋ฉด ๋ค์๊ณผ ๊ฐ์ด ๋ถํ ํ ์ ์๋ค. | ||
- [[...left], root๊ฐ, [...right]] | ||
- ์์ left, right๋ inorder ํธ๋ฆฌ์ ๊ฐ์ ๋ฐฉ์์ผ๋ก ๊ตฌ์ฑ๋๋ค. | ||
- ์ด๋, | ||
- left์ ์ฒซ ์์ดํ ์ด ์ธ๋ฑ์ค inorder_s์ ์๊ณ , | ||
- right์ ๋ง์ง๋ง ์์ดํ ์ด ์ธ๋ฑ์ค inorder_e - 1์ ์๋ค๊ณ ํ์. | ||
- ์ฆ, inorder_e๋ฅผ ๋ฏธํฌํจ! | ||
- preorder ํธ๋ฆฌ์ ๋งจ ์ ๊ฐ์ ํตํด root๊ฐ val์ ์ฐพ๊ณ , ์ด ๊ฐ์ผ๋ก inorder์ root๊ฐ์ ์ธ๋ฑ์ค๋ฅผ ์ฐพ์ ์ ์๋ค. | ||
- ๋ชจ๋ node์ val๊ฐ์ด uniqueํ ๊ฒ์ด ์กฐ๊ฑด์ผ๋ก ์ฃผ์ด์ ธ ์์ผ๋ฏ๋ก val๊ฐ์ indices๋ฅผ ์ ์ฒ๋ฆฌํด๋ ์ ์๋ค. | ||
- ์ด๋, inorder์ root๊ฐ์ ์ธ๋ฑ์ค๋ฅผ inorder_root์ด๋ผ๊ณ ํ์. | ||
- inorder์ root๊ฐ์ ์์น์ inorder ํธ๋ฆฌ์ ์์ ์์น๋ฅผ ์ ์ ์๋ค๋ฉด | ||
[...left]์ ๊ธธ์ด left_len์ ์ ์ ์๋ค. | ||
- left_len = inorder_root - inorder_start | ||
- preorder ํธ๋ฆฌ์ left์ ๋ฃจํธ๋ [...left]์ ์ฒซ ์์ดํ , ์ฆ, preorder_root์ 1์ ๋ํ ๊ฐ์ด๋ค. | ||
- preorder ํธ๋ฆฌ์ right์ ๋ฃจํธ๋ [...right]์ ์ฒซ ์์ดํ , ์ฆ, preorder_root + 1 + left_len์ด๋ค. | ||
- root๊ฐ์ ๊ตฌํ ์ ์์ผ๋ฉด ๋ ธ๋๊ฐ ์๋ค. | ||
- inorder_s >= inorder_e์ ๊ฐ์ด ํ๋ณ์ด ๊ฐ๋ฅํ๋ค. ์ฆ, ์์ดํ ์ด ํ๋๋ ์๋ ๊ฒฝ์ฐ. | ||
์์ ์์ด๋์ด๋ฅผ ์ข ํฉํ๋ฉด, | ||
- preorder ํธ๋ฆฌ์ ๋ฃจํธ ์ธ๋ฑ์ค preorder_root๊ฐ ์ฃผ์ด์ง, ๊ตฌ๊ฐ (inorder_s, inorder_e)์์ ์ ์๋ inorder ํธ๋ฆฌ๋ | ||
- val๊ฐ์ preorder[preorder_root]์ด ๋๋ค. | ||
- left node๋ ์๋์ ๊ฐ์ด ๊ตฌํด์ง๋ค. | ||
- preorder ํธ๋ฆฌ์ ๋ฃจํธ ์ธ๋ฑ์ค preorder_root + 1, | ||
- ๊ตฌ๊ฐ (inorder_s, inorder_root) | ||
- ์ด๋ ๊ตฌ๊ฐ์ด ์ ํจํ์ง ์์ผ๋ฉด ๋ ธ๋๊ฐ ์๋ค. | ||
- right node๋ ์๋์ ๊ฐ์ด ๊ตฌํด์ง๋ค. | ||
- preorder ํธ๋ฆฌ์ ๋ฃจํธ ์ธ๋ฑ์ค preorder_root + 1 + left_len, | ||
- ๊ตฌ๊ฐ (inorder_root + 1, inorder_end) | ||
- ์ด๋ ๊ตฌ๊ฐ์ด ์ ํจํ์ง ์์ผ๋ฉด ๋ ธ๋๊ฐ ์๋ค. | ||
SC: | ||
- ์ฒ์ inorder_indices๋ฅผ ๊ณ์ฐํ ๋ O(n). | ||
- ์๋์ buildํจ์ ํธ์ถ์ด ์ต๋ ํธ๋ฆฌ์ ๊น์ด๋งํผ ์ฌ๊ท๋ฅผ ๋๋ฉด์ ์์ผ ์ ์๋ค. | ||
- ํธ๋ฆฌ์ ๊น์ด๋ ์ต์ ์ ๊ฒฝ์ฐ O(n). | ||
TC: | ||
- buildํจ์๋ O(1). ์ฝ๋ ์ฐธ์กฐ. | ||
- ์์ ๊ณผ์ ์ n๊ฐ์ ๋ ธ๋์ ๋ํด ๋ฐ๋ณตํ๋ฏ๋ก O(n). | ||
""" | ||
|
||
|
||
# Definition for a binary tree node. | ||
# class TreeNode: | ||
# def __init__(self, val=0, left=None, right=None): | ||
# self.val = val | ||
# self.left = left | ||
# self.right = right | ||
class Solution: | ||
def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]: | ||
inorder_indices = {v: i for i, v in enumerate(inorder)} | ||
|
||
def build(inorder_s, inorder_e, preorder_root): | ||
if inorder_s >= inorder_e: # O(1) | ||
return None | ||
val = preorder[preorder_root] # O(1) | ||
inorder_root = inorder_indices[val] # O(1) | ||
left_len = inorder_root - inorder_s # O(1) | ||
return TreeNode( | ||
val, | ||
left=build(inorder_s, inorder_root, preorder_root + 1), | ||
right=build(inorder_root + 1, inorder_e, preorder_root + 1 + left_len), | ||
) | ||
|
||
return build(0, len(inorder), 0) | ||
|
||
|
||
""" | ||
๊ทธ๋ฐ๋ฐ ์์ ์์ด๋์ด๋ฅผ ๋ค์ ์๊ฐํด๋ณด๋ฉด, ๋ชจ๋ ๋ ธ๋๋ค์ preorder ์์๋ก ์ํํ๋ค! | ||
- `val = preorder[preorder_root]`์ ๊ฐ์ ๋ฐฉ์์ผ๋ก val๊ฐ์ ๊ตฌํ์ง ์๊ณ , ์ฃผ์ด์ง preorder๋ฅผ ์์๋๋ก ๊ฐ์ ธ์๋ ๋จ. | ||
- ์ฆ, preorder๋ฅผ iterator๋ก ๋ฐ๊ฟ์ next๋ฅผ ํตํด ๊ฐ์ ํ๋์ฉ ๋ฝ์์์ ๊ฑด๋ค์ค๋ ๋๋ค. | ||
- ์ด๋ ๊ฒ ํ๋ฉด buildํจ์์ preorder_root๋ฅผ ์ ๋ฌํ์ง ์์๋ ๋จ. | ||
""" | ||
|
||
|
||
class Solution: | ||
def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]: | ||
inorder_indices = {v: i for i, v in enumerate(inorder)} | ||
preorder_iter = iter(preorder) | ||
|
||
def build(inorder_s, inorder_e): | ||
if inorder_s >= inorder_e: # O(1) | ||
return None | ||
val = next(preorder_iter) # O(1) | ||
inorder_root = inorder_indices[val] # O(1) | ||
return TreeNode( | ||
val, | ||
left=build(inorder_s, inorder_root), | ||
right=build(inorder_root + 1, inorder_e), | ||
) | ||
|
||
return build(0, len(inorder)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
"""TC: O(n), SC: O(n) | ||
์์ด๋์ด: | ||
- bin์ผ๋ก ๋ณํํ ๊ฐ์ ๊ธธ์ด๊ฐ k์ธ ๋ชจ๋ ์๋ค์ ๋ํ bit_count๊ฐ์ ์๊ณ ์๋ค๊ณ ํ์. | ||
- ์ฌ์ค ์์ ์๋ค์ bin์ผ๋ก ๋ณํํ์๋ ๋งจ ์ ์๋ฆฌ๊ฐ 0์ผ๋ก ์์ํ๋ ๊ธธ์ด k+1์ ์๋ผ๊ณ ๋ณผ ์ ์๋ค. | ||
- ๊ทธ๋ ๋ค๋ฉด bin์ผ๋ก ๋ณํํ์๋ ๋งจ ์ ์๋ฆฌ๊ฐ 1๋ก ์์ํ๋ ๊ธธ์ด k+1์ธ ์๋ค์ bit_count๋ | ||
๋งจ ์ ์๋ฆฌ๊ฐ 0์ผ๋ก ์์ํ๋ ์๋ค์ bit_count์ 1์ ๋ํ ๊ฒ์ด๋ผ๊ณ ํ ์ ์๋ค. | ||
- ์์ ์์ด๋์ด๋ฅผ ํ์ฉํ๋ฉด ์ 2^k ์๋ค์ bit_count๋ฅผ ์๊ณ ์์ผ๋ฉด ๊ฐ๋จํ ๋ํ๊ธฐ ์ฐ์ฐ์ ํตํด | ||
์ดํ 2^k ์๋ค์ bit_count๊ฐ๋ ์ ์ ์๋ค. | ||
e.g.) | ||
- 0, 1์ bit_count๊ฐ [0, 1]์ด๋ผ๋ฉด, 2, 3์ bit_count๋ [0+1, 1+1], ์ฆ, 0~3์ bit_count๋ [0, 1, 1, 2] | ||
- 0~3์ bit_count๊ฐ [0, 1, 1, 2]๋ผ๋ฉด, 4~7์ bit_count๋ [1, 2, 2, 3], ์ฆ, 0~7์ bit_count๋ | ||
[0, 1, 1, 2, 1, 2, 2, 3] | ||
- ... | ||
- ๋ฆฌ์คํธ์ ํฌ๊ธฐ๋ฅผ 2๋ฐฐ์ฉ ๋๋ฆฌ๋ค๊ฐ n๋ณด๋ค ์ปค์ก์๋ ์ n๊ฐ์ ์์ดํ ๋ง ์ทจํด์ ๋ฆฌํด. | ||
SC: | ||
- ์๋์์ ๋ฆฌ์คํธ s์ ๊ธธ์ด๋ 2^(k-1) < n <= 2^k๋ฅผ ๋ง์กฑํ๋ 2^k๋งํผ ์ปค์ง๋ค. | ||
- ์ฆ, O(n). | ||
TC: | ||
- s ์์ ๋ค์ด์๋ i๋ฒ์งธ ์์ดํ ์ ๊ณ์ฐํ ๋ ํ์ํ ์ฐ์ฐ์ ๋ง์ 1ํ, ์ฆ, O(1). | ||
- i๋ฒ์งธ ์์ดํ ๊ฐ์ ๊ตฌํ๊ธฐ ์ํด ๊ทธ ์์ ๊ฐ์ ๋ฏธ๋ฆฌ ๊ณ์ฐํด๋ ๊ฒ์ด๋ผ ์๊ฐํ ์ ์๋ค. | ||
- SC ๋ถ์๊ณผ ๋น์ทํ๊ฒ, 2^(k-1) < n <= 2^k๋ฅผ ๋ง์กฑํ๋ 2^k๋งํผ ๋ฐ๋ณต. ์ฆ, O(n). | ||
""" | ||
|
||
|
||
class Solution: | ||
def countBits(self, n: int) -> List[int]: | ||
s = [0] | ||
m = n * 2 | ||
while m := m >> 1: | ||
s += [i + 1 for i in s] | ||
return s[: n + 1] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
"""TC: O(n), SC: O(1) | ||
์์ด๋์ด: | ||
๋ท k๊ฐ์ ๊ธ์๋ฅผ ๋์ฝ๋ฉ ํ๋ ๊ฒฝ์ฐ์ ์๋ฅผ f(k)๋ผ๊ณ ํ์. | ||
f(k)๋ ๋ค์์ ๋ ๊ฒฝ์ฐ์ ์๋ฅผ ๋ํ ๊ฐ์ด๋ค. | ||
- ๋ท k๊ฐ์ ๊ธ์ ์ค ์ฒซ ๊ธ์๊ฐ ๋์ฝ๋ฉ ๊ฐ๋ฅํ ๊ฒฝ์ฐ, ๋ท k-1๊ธ์๋ฅผ ๋์ฝ๋ฉํ๋ ๊ฒฝ์ฐ์ ์ | ||
- ๋ท k๊ฐ์ ๊ธ์ ์ค ์ ๋ ๊ธ์๊ฐ ๋์ฝ๋ฉ ๊ฐ๋ฅํ ๊ฒฝ์ฐ, ๋ท k-2๊ธ์๋ฅผ ๋์ฝ๋ฉํ๋ ๊ฒฝ์ฐ์ ์ | ||
์ฆ, f(k) = (์ ๋ ๊ธ์ ํ๋ณ)*f(k-2) + (์ ํ ๊ธ์ ํ๋ณ)*f(k-1) | ||
SC: | ||
- tabulation ๊ณผ์ ์์ ๊ฐ 2๊ฐ๋ง ๊ณ์ ์ ์งํ๋ค. | ||
- ์ฆ, O(1). | ||
TC: | ||
- f(k) ๊ตฌํ๋ ์: O(1) | ||
- ๋ ๊ธ์๊ฐ ๋์ฝ๋ฉ ๊ฐ๋ฅํ์ง ํ๋ณ: O(1) | ||
- ์ฒซ ๊ธ์๊ฐ ๋์ฝ๋ฉ ๊ฐ๋ฅํ์ง ํ๋ณ: O(1) | ||
- ์์ f(k)๋ฅผ ๊ตฌํ๋ ๊ฒ์ s์ ๊ธธ์ด์์ 2๋ฅผ ๋บ ์๋งํผ ๋ฃจํ, ์ฆ, O(n) | ||
- ์ข ํฉํ๋ฉด O(n). | ||
""" | ||
|
||
|
||
class Solution: | ||
def numDecodings(self, s: str) -> int: | ||
# init | ||
x, y = 1, int(int(s[-1]) != 0) # f(0), f(1) | ||
# tabulation | ||
for i in range(len(s) - 2, -1, -1): # ๋ท k๊ฐ ๊ธ์์ ์์ ๊ธ์๊ฐ s[i] | ||
# f(k-2), f(k-1)์ f(k-1), f(k)๋ก | ||
x, y = y, (x * (10 <= int(s[i : i + 2]) <= 26)) + (y * (int(s[i]) != 0)) | ||
return y |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
""" | ||
encode: TC: O(n), SC: O(l) | ||
decode: TC: O(n), SC: O(l) | ||
input์ผ๋ก ๋ค์ด์จ string๋ค์ ๊ธธ์ด๋ฅผ ์ ๋ถ ๋ํ ๊ฐ์ l์ด๋ผ๊ณ ํ์. | ||
input์ ๋ค์ด์๋ ์์ดํ ๊ฐ์๋ฅผ n์ด๋ผ๊ณ ํ์. | ||
์์ด๋์ด: | ||
- ์ธํ์ ์ฒ๋ฆฌํ ๊ฐ ์์ชฝ์ ์ธํ์ ํด์ํ๊ธฐ ์ํ ์ ๋ณด๋ฅผ ๋์. | ||
- ์ฆ, ์ธํ์ ์ฒ๋ฆฌํ ๊ฐ์ ์ผ์ข ์ body๋ก ๋ณด๊ณ ์์ header๋ฅผ ๋ถ์ด๋ ์ ๊ทผ. | ||
- header๋ body์ ๊ฐ์ด ์ด๋ป๊ฒ ๋ค์ด์๋ ์ํฅ์ ๋ฐ์ง ์๋๋ค! | ||
- ๋ค์๊ณผ ๊ฐ์ด encode๋ฅผ ํ๋ค. | ||
- encodeํ string์ f'{header}:{body}'ํ์์ผ๋ก ๋์ด์๋ค. | ||
- body์๋ input์ ์๋ ๊ฐ์ ๋ฐ๋ก concatํ ๊ฐ์ ์ด๋ค. | ||
- header์๋ input์ ์๋ string์ ๊ธธ์ด๋ฅผ ','๋ก ๊ตฌ๋ถํ ๊ฐ์ ์ด๋ค. | ||
- e.g.) | ||
body: ['a', 'bc', 'd'] -> 'abcd' | ||
header: ['a', 'bc', 'd'] -> '1,2,1' | ||
encoded string: '1,2,1:abcd' | ||
- ๋ค์๊ณผ ๊ฐ์ด decode๋ฅผ ํ๋ค. | ||
- ์ฒซ ๋ฒ์งธ๋ก ๋ฑ์ฅํ๋ ':'์ ์ฐพ์์ splitํ๋ค. | ||
- ์ ๋ถ๋ถ์ด header, ๋ท ๋ถ๋ถ์ด body๋ค. | ||
- header๋ฅผ ๋ค์๊ณผ ๊ฐ์ด ์ฒ๋ฆฌํ๋ค. | ||
- ','๋ก splitํด์ `x: List[int]`๋ฅผ ์ป๋๋ค. | ||
- body๋ฅผ ๋ค์๊ณผ ๊ฐ์ด ์ฒ๋ฆฌํ๋ค. | ||
- body๋ฅผ ์์ ๊ตฌํ x์ ๋ค์ด์๋ ๊ธธ์ด๋ก ์ญ ์ชผ๊ฐ๋ฉด ๋๋ค. | ||
- x์ ๋์ ํฉ์ ๊ตฌํ๋ฉด์ ์ด๋ฅผ ์์ ์ธ๋ฑ์ค๋ก ํ์ฉํ๋ค. | ||
SC: | ||
- encode | ||
- body์ ๊ธธ์ด๋ l์ด๋ค. ์ฆ, O(l). | ||
- header์ ๊ธธ์ด๋ ์ต์ ์ ๊ฒฝ์ฐ O(l)์ด๋ค. | ||
- input์ ๋ชจ๋ ๊ธ์๊ฐ ๊ธธ์ด 1์ผ๋, ์ต์ ์ ๊ฒฝ์ฐ O(l). | ||
- input์ ๋ชจ๋ ๊ธ์๊ฐ ํ ๋จ์ด์ผ๋, ์ต๊ณ ์ ๊ฒฝ์ฐ O(log l). | ||
- ์ข ํฉํ๋ฉด O(l) + O(l)์ด๋ผ O(l)์ด๋ค. | ||
- decode | ||
- ์ ์ฒด ๋ฉ์์ง์ body์ ๋ค์ด์๋ ๊ฐ์ ์ชผ๊ฐ์ ๋ฆฌ์คํธ๋ก ๋ง๋๋ ๊ฒ์ด๋ฏ๋ก O(l). | ||
- ๊ธธ์ด ๊ฐ์ splitํด์ ๋ฆฌ์คํธ๋ก ๋ง๋ ๋ค. O(n) | ||
- ์ข ํฉํ๋ฉด O(l + n)์ธ๋ฐ, ์ด๋ n์ ๋ฌด์กฐ๊ฑด l ์ดํ์ด๋ฏ๋ก O(l). | ||
TC: | ||
- encode | ||
- n๊ฐ์ ์์ดํ ์ ์ํํ๋ฉด์ ๊ธธ์ด๋ฅผ ์ป๋๋ค. O(n). | ||
- decode | ||
- ๊ธธ์ด ๊ฐ์ splitํด์ ๋ฆฌ์คํธ๋ก ๋ง๋ ๋ค. O(n). | ||
- ๋์ ํฉ์ ํ์ฉํ์ฌ ๊ธธ์ด ๊ฐ ๋ฆฌ์คํธ๋ฅผ ํ ๋ฒ ์ํ. O(n). | ||
- ์ข ํฉํ๋ฉด O(n). | ||
""" | ||
|
||
|
||
class Solution: | ||
""" | ||
@param: strs: a list of strings | ||
@return: encodes a list of strings to a single string. | ||
""" | ||
|
||
def encode(self, strs): | ||
body = "".join(strs) | ||
header = ",".join([str(len(i)) for i in strs]) | ||
return header + ":" + body | ||
|
||
""" | ||
@param: str: A string | ||
@return: decodes a single string to a list of strings | ||
""" | ||
|
||
def decode(self, str): | ||
header, body = str.split(":", 1) | ||
len_list = [int(i) for i in header.split(",")] | ||
start_ind = 0 | ||
result = [] | ||
for i in len_list: | ||
result.append(body[start_ind : start_ind + i]) | ||
start_ind += i | ||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
"""TC: O(n), SC: O(n) | ||
์ฌ๊ธฐ์ n์ s, t์ ๊ธธ์ด๊ฐ ์ค ํฐ ๊ฒ์ด๋ผ ๊ฐ์ . | ||
SC: | ||
- Counter๋ s, t์ ๋ค์ด์๋ ๊ธ์๋ค์ key๋ก ํ๋ dict. ์ฆ, SC๋ O(n). | ||
TC: | ||
- s, t์ ๋ค์ด์๋ ๊ธ์๋ค์ key๋ก dict๋ฅผ ์ ๋ฐ์ดํธ๋ฅผ ํ ๋ฒ ํ ๋ O(1). | ||
- ์์ ๊ณผ์ ์ ๊ธธ์ด n๋งํผ ๋ฐ๋ณตํ๋ฏ๋ก O(n). | ||
""" | ||
|
||
from collections import Counter | ||
|
||
|
||
class Solution: | ||
def isAnagram(self, s: str, t: str) -> bool: | ||
# return sorted(s) == sorted(t) # TC: O(n log n), SC: O(n) | ||
return Counter(s) == Counter(t) # TC: O(n), SC: O(n) |