Skip to content

Commit c554369

Browse files
committed
solutions
1 parent e55594c commit c554369

File tree

5 files changed

+259
-0
lines changed

5 files changed

+259
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
"""TC: O(n), SC: O(n)
2+
3+
์•„์ด๋””์–ด:
4+
- preorder ํŠธ๋ฆฌ๊ฐ€ ์ฃผ์–ด์ ธ ์žˆ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ถ„ํ• ํ•  ์ˆ˜ ์žˆ๋‹ค.
5+
- [root๊ฐ’, [...left], [...right]]
6+
- ์œ„์˜ left, right๋Š” preorder ํŠธ๋ฆฌ์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ๊ตฌ์„ฑ๋œ๋‹ค.
7+
- inorder ํŠธ๋ฆฌ๊ฐ€ ์ฃผ์–ด์ ธ ์žˆ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ถ„ํ• ํ•  ์ˆ˜ ์žˆ๋‹ค.
8+
- [[...left], root๊ฐ’, [...right]]
9+
- ์œ„์˜ left, right๋Š” inorder ํŠธ๋ฆฌ์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ๊ตฌ์„ฑ๋œ๋‹ค.
10+
- ์ด๋•Œ,
11+
- left์˜ ์ฒซ ์•„์ดํ…œ์ด ์ธ๋ฑ์Šค inorder_s์— ์žˆ๊ณ ,
12+
- right์˜ ๋งˆ์ง€๋ง‰ ์•„์ดํ…œ์ด ์ธ๋ฑ์Šค inorder_e - 1์— ์žˆ๋‹ค๊ณ  ํ•˜์ž.
13+
- ์ฆ‰, inorder_e๋ฅผ ๋ฏธํฌํ•จ!
14+
- preorder ํŠธ๋ฆฌ์˜ ๋งจ ์•ž ๊ฐ’์„ ํ†ตํ•ด root๊ฐ’ val์„ ์ฐพ๊ณ , ์ด ๊ฐ’์œผ๋กœ inorder์˜ root๊ฐ’์˜ ์ธ๋ฑ์Šค๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค.
15+
- ๋ชจ๋“  node์˜ val๊ฐ’์ด uniqueํ•œ ๊ฒƒ์ด ์กฐ๊ฑด์œผ๋กœ ์ฃผ์–ด์ ธ ์žˆ์œผ๋ฏ€๋กœ val๊ฐ’์˜ indices๋ฅผ ์ „์ฒ˜๋ฆฌํ•ด๋‘˜ ์ˆ˜ ์žˆ๋‹ค.
16+
- ์ด๋•Œ, inorder์˜ root๊ฐ’์˜ ์ธ๋ฑ์Šค๋ฅผ inorder_root์ด๋ผ๊ณ  ํ•˜์ž.
17+
- inorder์˜ root๊ฐ’์˜ ์œ„์น˜์™€ inorder ํŠธ๋ฆฌ์˜ ์‹œ์ž‘ ์œ„์น˜๋ฅผ ์•Œ ์ˆ˜ ์žˆ๋‹ค๋ฉด
18+
[...left]์˜ ๊ธธ์ด left_len์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.
19+
- left_len = inorder_root - inorder_start
20+
- preorder ํŠธ๋ฆฌ์˜ left์˜ ๋ฃจํŠธ๋Š” [...left]์˜ ์ฒซ ์•„์ดํ…œ, ์ฆ‰, preorder_root์— 1์„ ๋”ํ•œ ๊ฐ’์ด๋‹ค.
21+
- preorder ํŠธ๋ฆฌ์˜ right์˜ ๋ฃจํŠธ๋Š” [...right]์˜ ์ฒซ ์•„์ดํ…œ, ์ฆ‰, preorder_root + 1 + left_len์ด๋‹ค.
22+
- root๊ฐ’์„ ๊ตฌํ•  ์ˆ˜ ์—†์œผ๋ฉด ๋…ธ๋“œ๊ฐ€ ์—†๋‹ค.
23+
- inorder_s >= inorder_e์™€ ๊ฐ™์ด ํŒ๋ณ„์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ์ฆ‰, ์•„์ดํ…œ์ด ํ•˜๋‚˜๋„ ์—†๋Š” ๊ฒฝ์šฐ.
24+
25+
์œ„์˜ ์•„์ด๋””์–ด๋ฅผ ์ข…ํ•ฉํ•˜๋ฉด,
26+
- preorder ํŠธ๋ฆฌ์˜ ๋ฃจํŠธ ์ธ๋ฑ์Šค preorder_root๊ฐ€ ์ฃผ์–ด์ง„, ๊ตฌ๊ฐ„ (inorder_s, inorder_e)์—์„œ ์ •์˜๋œ inorder ํŠธ๋ฆฌ๋Š”
27+
- val๊ฐ’์€ preorder[preorder_root]์ด ๋œ๋‹ค.
28+
- left node๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ๊ตฌํ•ด์ง„๋‹ค.
29+
- preorder ํŠธ๋ฆฌ์˜ ๋ฃจํŠธ ์ธ๋ฑ์Šค preorder_root + 1,
30+
- ๊ตฌ๊ฐ„ (inorder_s, inorder_root)
31+
- ์ด๋•Œ ๊ตฌ๊ฐ„์ด ์œ ํšจํ•˜์ง€ ์•Š์œผ๋ฉด ๋…ธ๋“œ๊ฐ€ ์—†๋‹ค.
32+
- right node๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ๊ตฌํ•ด์ง„๋‹ค.
33+
- preorder ํŠธ๋ฆฌ์˜ ๋ฃจํŠธ ์ธ๋ฑ์Šค preorder_root + 1 + left_len,
34+
- ๊ตฌ๊ฐ„ (inorder_root + 1, inorder_end)
35+
- ์ด๋•Œ ๊ตฌ๊ฐ„์ด ์œ ํšจํ•˜์ง€ ์•Š์œผ๋ฉด ๋…ธ๋“œ๊ฐ€ ์—†๋‹ค.
36+
37+
38+
SC:
39+
- ์ฒ˜์Œ inorder_indices๋ฅผ ๊ณ„์‚ฐํ• ๋•Œ O(n).
40+
- ์•„๋ž˜์˜ buildํ•จ์ˆ˜ ํ˜ธ์ถœ์ด ์ตœ๋Œ€ ํŠธ๋ฆฌ์˜ ๊นŠ์ด๋งŒํผ ์žฌ๊ท€๋ฅผ ๋Œ๋ฉด์„œ ์Œ“์ผ ์ˆ˜ ์žˆ๋‹ค.
41+
- ํŠธ๋ฆฌ์˜ ๊นŠ์ด๋Š” ์ตœ์•…์˜ ๊ฒฝ์šฐ O(n).
42+
43+
TC:
44+
- buildํ•จ์ˆ˜๋Š” O(1). ์ฝ”๋“œ ์ฐธ์กฐ.
45+
- ์œ„์˜ ๊ณผ์ •์„ n๊ฐœ์˜ ๋…ธ๋“œ์— ๋Œ€ํ•ด ๋ฐ˜๋ณตํ•˜๋ฏ€๋กœ O(n).
46+
"""
47+
48+
49+
# Definition for a binary tree node.
50+
# class TreeNode:
51+
# def __init__(self, val=0, left=None, right=None):
52+
# self.val = val
53+
# self.left = left
54+
# self.right = right
55+
class Solution:
56+
def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
57+
inorder_indices = {v: i for i, v in enumerate(inorder)}
58+
59+
def build(inorder_s, inorder_e, preorder_root):
60+
if inorder_s >= inorder_e: # O(1)
61+
return None
62+
val = preorder[preorder_root] # O(1)
63+
inorder_root = inorder_indices[val] # O(1)
64+
left_len = inorder_root - inorder_s # O(1)
65+
return TreeNode(
66+
val,
67+
left=build(inorder_s, inorder_root, preorder_root + 1),
68+
right=build(inorder_root + 1, inorder_e, preorder_root + 1 + left_len),
69+
)
70+
71+
return build(0, len(inorder), 0)
72+
73+
74+
"""
75+
๊ทธ๋Ÿฐ๋ฐ ์œ„์˜ ์•„์ด๋””์–ด๋ฅผ ๋‹ค์‹œ ์ƒ๊ฐํ•ด๋ณด๋ฉด, ๋ชจ๋“  ๋…ธ๋“œ๋“ค์„ preorder ์ˆœ์„œ๋กœ ์ˆœํšŒํ•œ๋‹ค!
76+
- `val = preorder[preorder_root]`์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ val๊ฐ’์„ ๊ตฌํ•˜์ง€ ์•Š๊ณ , ์ฃผ์–ด์ง„ preorder๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ๊ฐ€์ ธ์™€๋„ ๋จ.
77+
- ์ฆ‰, preorder๋ฅผ iterator๋กœ ๋ฐ”๊ฟ”์„œ next๋ฅผ ํ†ตํ•ด ๊ฐ’์„ ํ•˜๋‚˜์”ฉ ๋ฝ‘์•„์™€์„œ ๊ฑด๋„ค์ค˜๋„ ๋œ๋‹ค.
78+
- ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด buildํ•จ์ˆ˜์— preorder_root๋ฅผ ์ „๋‹ฌํ•˜์ง€ ์•Š์•„๋„ ๋จ.
79+
"""
80+
81+
82+
class Solution:
83+
def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
84+
inorder_indices = {v: i for i, v in enumerate(inorder)}
85+
preorder_iter = iter(preorder)
86+
87+
def build(inorder_s, inorder_e):
88+
if inorder_s >= inorder_e: # O(1)
89+
return None
90+
val = next(preorder_iter) # O(1)
91+
inorder_root = inorder_indices[val] # O(1)
92+
return TreeNode(
93+
val,
94+
left=build(inorder_s, inorder_root),
95+
right=build(inorder_root + 1, inorder_e),
96+
)
97+
98+
return build(0, len(inorder))

โ€Žcounting-bits/haklee.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""TC: O(n), SC: O(n)
2+
3+
์•„์ด๋””์–ด:
4+
- bin์œผ๋กœ ๋ณ€ํ™˜ํ•œ ๊ฐ’์˜ ๊ธธ์ด๊ฐ€ k์ธ ๋ชจ๋“  ์ˆ˜๋“ค์— ๋Œ€ํ•œ bit_count๊ฐ’์„ ์•Œ๊ณ  ์žˆ๋‹ค๊ณ  ํ•˜์ž.
5+
- ์‚ฌ์‹ค ์œ„์˜ ์ˆ˜๋“ค์€ bin์œผ๋กœ ๋ณ€ํ™˜ํ–ˆ์„๋•Œ ๋งจ ์•ž ์ž๋ฆฌ๊ฐ€ 0์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” ๊ธธ์ด k+1์˜ ์ˆ˜๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค.
6+
- ๊ทธ๋ ‡๋‹ค๋ฉด bin์œผ๋กœ ๋ณ€ํ™˜ํ–ˆ์„๋•Œ ๋งจ ์•ž ์ž๋ฆฌ๊ฐ€ 1๋กœ ์‹œ์ž‘ํ•˜๋Š” ๊ธธ์ด k+1์ธ ์ˆ˜๋“ค์˜ bit_count๋Š”
7+
๋งจ ์•ž ์ž๋ฆฌ๊ฐ€ 0์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” ์ˆ˜๋“ค์˜ bit_count์— 1์„ ๋”ํ•œ ๊ฒƒ์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค.
8+
- ์œ„์˜ ์•„์ด๋””์–ด๋ฅผ ํ™œ์šฉํ•˜๋ฉด ์•ž 2^k ์ˆ˜๋“ค์˜ bit_count๋ฅผ ์•Œ๊ณ  ์žˆ์œผ๋ฉด ๊ฐ„๋‹จํ•œ ๋”ํ•˜๊ธฐ ์—ฐ์‚ฐ์„ ํ†ตํ•ด
9+
์ดํ›„ 2^k ์ˆ˜๋“ค์˜ bit_count๊ฐ’๋„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.
10+
e.g.)
11+
- 0, 1์˜ bit_count๊ฐ€ [0, 1]์ด๋ผ๋ฉด, 2, 3์˜ bit_count๋Š” [0+1, 1+1], ์ฆ‰, 0~3์˜ bit_count๋Š” [0, 1, 1, 2]
12+
- 0~3์˜ bit_count๊ฐ€ [0, 1, 1, 2]๋ผ๋ฉด, 4~7์˜ bit_count๋Š” [1, 2, 2, 3], ์ฆ‰, 0~7์˜ bit_count๋Š”
13+
[0, 1, 1, 2, 1, 2, 2, 3]
14+
- ...
15+
- ๋ฆฌ์ŠคํŠธ์˜ ํฌ๊ธฐ๋ฅผ 2๋ฐฐ์”ฉ ๋Š˜๋ฆฌ๋‹ค๊ฐ€ n๋ณด๋‹ค ์ปค์กŒ์„๋•Œ ์•ž n๊ฐœ์˜ ์•„์ดํ…œ๋งŒ ์ทจํ•ด์„œ ๋ฆฌํ„ด.
16+
17+
18+
SC:
19+
- ์•„๋ž˜์—์„œ ๋ฆฌ์ŠคํŠธ s์˜ ๊ธธ์ด๋Š” 2^(k-1) < n <= 2^k๋ฅผ ๋งŒ์กฑํ•˜๋Š” 2^k๋งŒํผ ์ปค์ง„๋‹ค.
20+
- ์ฆ‰, O(n).
21+
22+
TC:
23+
- s ์•ˆ์— ๋“ค์–ด์žˆ๋Š” i๋ฒˆ์งธ ์•„์ดํ…œ์„ ๊ณ„์‚ฐํ• ๋•Œ ํ•„์š”ํ•œ ์—ฐ์‚ฐ์€ ๋ง์…ˆ 1ํšŒ, ์ฆ‰, O(1).
24+
- i๋ฒˆ์งธ ์•„์ดํ…œ ๊ฐ’์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด ๊ทธ ์•ž์˜ ๊ฐ’์„ ๋ฏธ๋ฆฌ ๊ณ„์‚ฐํ•ด๋‘” ๊ฒƒ์ด๋ผ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๋‹ค.
25+
- SC ๋ถ„์„๊ณผ ๋น„์Šทํ•˜๊ฒŒ, 2^(k-1) < n <= 2^k๋ฅผ ๋งŒ์กฑํ•˜๋Š” 2^k๋งŒํผ ๋ฐ˜๋ณต. ์ฆ‰, O(n).
26+
"""
27+
28+
29+
class Solution:
30+
def countBits(self, n: int) -> List[int]:
31+
s = [0]
32+
m = n * 2
33+
while m := m >> 1:
34+
s += [i + 1 for i in s]
35+
return s[: n + 1]

โ€Ždecode-ways/haklee.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""TC: O(n), SC: O(1)
2+
3+
์•„์ด๋””์–ด:
4+
๋’ท k๊ฐœ์˜ ๊ธ€์ž๋ฅผ ๋””์ฝ”๋”ฉ ํ•˜๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ f(k)๋ผ๊ณ  ํ•˜์ž.
5+
f(k)๋Š” ๋‹ค์Œ์˜ ๋‘ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋”ํ•œ ๊ฐ’์ด๋‹ค.
6+
- ๋’ท k๊ฐœ์˜ ๊ธ€์ž ์ค‘ ์ฒซ ๊ธ€์ž๊ฐ€ ๋””์ฝ”๋”ฉ ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ, ๋’ท k-1๊ธ€์ž๋ฅผ ๋””์ฝ”๋”ฉํ•˜๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜
7+
- ๋’ท k๊ฐœ์˜ ๊ธ€์ž ์ค‘ ์•ž ๋‘ ๊ธ€์ž๊ฐ€ ๋””์ฝ”๋”ฉ ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ, ๋’ท k-2๊ธ€์ž๋ฅผ ๋””์ฝ”๋”ฉํ•˜๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜
8+
์ฆ‰, f(k) = (์•ž ๋‘ ๊ธ€์ž ํŒ๋ณ„)*f(k-2) + (์•ž ํ•œ ๊ธ€์ž ํŒ๋ณ„)*f(k-1)
9+
10+
11+
SC:
12+
- tabulation ๊ณผ์ •์—์„œ ๊ฐ’ 2๊ฐœ๋งŒ ๊ณ„์† ์œ ์ง€ํ•œ๋‹ค.
13+
- ์ฆ‰, O(1).
14+
15+
TC:
16+
- f(k) ๊ตฌํ•˜๋Š” ์‹: O(1)
17+
- ๋‘ ๊ธ€์ž๊ฐ€ ๋””์ฝ”๋”ฉ ๊ฐ€๋Šฅํ•œ์ง€ ํŒ๋ณ„: O(1)
18+
- ์ฒซ ๊ธ€์ž๊ฐ€ ๋””์ฝ”๋”ฉ ๊ฐ€๋Šฅํ•œ์ง€ ํŒ๋ณ„: O(1)
19+
- ์œ„์˜ f(k)๋ฅผ ๊ตฌํ•˜๋Š” ๊ฒƒ์„ s์˜ ๊ธธ์ด์—์„œ 2๋ฅผ ๋บ€ ์ˆ˜๋งŒํผ ๋ฃจํ”„, ์ฆ‰, O(n)
20+
- ์ข…ํ•ฉํ•˜๋ฉด O(n).
21+
"""
22+
23+
24+
class Solution:
25+
def numDecodings(self, s: str) -> int:
26+
# init
27+
x, y = 1, int(int(s[-1]) != 0) # f(0), f(1)
28+
# tabulation
29+
for i in range(len(s) - 2, -1, -1): # ๋’ท k๊ฐœ ๊ธ€์ž์˜ ์‹œ์ž‘ ๊ธ€์ž๊ฐ€ s[i]
30+
# f(k-2), f(k-1)์„ f(k-1), f(k)๋กœ
31+
x, y = y, (x * (10 <= int(s[i : i + 2]) <= 26)) + (y * (int(s[i]) != 0))
32+
return y
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
"""
2+
encode: TC: O(n), SC: O(l)
3+
decode: TC: O(n), SC: O(l)
4+
5+
input์œผ๋กœ ๋“ค์–ด์˜จ string๋“ค์˜ ๊ธธ์ด๋ฅผ ์ „๋ถ€ ๋”ํ•œ ๊ฐ’์„ l์ด๋ผ๊ณ  ํ•˜์ž.
6+
input์— ๋“ค์–ด์žˆ๋Š” ์•„์ดํ…œ ๊ฐœ์ˆ˜๋ฅผ n์ด๋ผ๊ณ  ํ•˜์ž.
7+
8+
์•„์ด๋””์–ด:
9+
- ์ธํ’‹์„ ์ฒ˜๋ฆฌํ•œ ๊ฐ’ ์•ž์ชฝ์— ์ธํ’‹์„ ํ•ด์„ํ•˜๊ธฐ ์œ„ํ•œ ์ •๋ณด๋ฅผ ๋‘์ž.
10+
- ์ฆ‰, ์ธํ’‹์„ ์ฒ˜๋ฆฌํ•œ ๊ฐ’์„ ์ผ์ข…์˜ body๋กœ ๋ณด๊ณ  ์•ž์— header๋ฅผ ๋ถ™์ด๋Š” ์ ‘๊ทผ.
11+
- header๋Š” body์˜ ๊ฐ’์ด ์–ด๋–ป๊ฒŒ ๋“ค์–ด์™€๋„ ์˜ํ–ฅ์„ ๋ฐ›์ง€ ์•Š๋Š”๋‹ค!
12+
- ๋‹ค์Œ๊ณผ ๊ฐ™์ด encode๋ฅผ ํ•œ๋‹ค.
13+
- encodeํ•œ string์€ f'{header}:{body}'ํ˜•์‹์œผ๋กœ ๋˜์–ด์žˆ๋‹ค.
14+
- body์—๋Š” input์— ์žˆ๋Š” ๊ฐ’์„ ๋ฐ”๋กœ concatํ•œ ๊ฐ’์„ ์“ด๋‹ค.
15+
- header์—๋Š” input์— ์žˆ๋Š” string์˜ ๊ธธ์ด๋ฅผ ','๋กœ ๊ตฌ๋ถ„ํ•œ ๊ฐ’์„ ์“ด๋‹ค.
16+
- e.g.)
17+
body: ['a', 'bc', 'd'] -> 'abcd'
18+
header: ['a', 'bc', 'd'] -> '1,2,1'
19+
encoded string: '1,2,1:abcd'
20+
- ๋‹ค์Œ๊ณผ ๊ฐ™์ด decode๋ฅผ ํ•œ๋‹ค.
21+
- ์ฒซ ๋ฒˆ์งธ๋กœ ๋“ฑ์žฅํ•˜๋Š” ':'์„ ์ฐพ์•„์„œ splitํ•œ๋‹ค.
22+
- ์•ž ๋ถ€๋ถ„์ด header, ๋’ท ๋ถ€๋ถ„์ด body๋‹ค.
23+
- header๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฒ˜๋ฆฌํ•œ๋‹ค.
24+
- ','๋กœ splitํ•ด์„œ `x: List[int]`๋ฅผ ์–ป๋Š”๋‹ค.
25+
- body๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฒ˜๋ฆฌํ•œ๋‹ค.
26+
- body๋ฅผ ์•ž์„œ ๊ตฌํ•œ x์— ๋“ค์–ด์žˆ๋Š” ๊ธธ์ด๋กœ ์ญ‰ ์ชผ๊ฐœ๋ฉด ๋œ๋‹ค.
27+
- x์˜ ๋ˆ„์ ํ•ฉ์„ ๊ตฌํ•˜๋ฉด์„œ ์ด๋ฅผ ์‹œ์ž‘ ์ธ๋ฑ์Šค๋กœ ํ™œ์šฉํ•œ๋‹ค.
28+
29+
SC:
30+
- encode
31+
- body์˜ ๊ธธ์ด๋Š” l์ด๋‹ค. ์ฆ‰, O(l).
32+
- header์˜ ๊ธธ์ด๋Š” ์ตœ์•…์˜ ๊ฒฝ์šฐ O(l)์ด๋‹ค.
33+
- input์˜ ๋ชจ๋“  ๊ธ€์ž๊ฐ€ ๊ธธ์ด 1์ผ๋•Œ, ์ตœ์•…์˜ ๊ฒฝ์šฐ O(l).
34+
- input์˜ ๋ชจ๋“  ๊ธ€์ž๊ฐ€ ํ•œ ๋‹จ์–ด์ผ๋•Œ, ์ตœ๊ณ ์˜ ๊ฒฝ์šฐ O(log l).
35+
- ์ข…ํ•ฉํ•˜๋ฉด O(l) + O(l)์ด๋ผ O(l)์ด๋‹ค.
36+
- decode
37+
- ์ „์ฒด ๋ฉ”์‹œ์ง€์˜ body์— ๋“ค์–ด์žˆ๋Š” ๊ฐ’์„ ์ชผ๊ฐœ์„œ ๋ฆฌ์ŠคํŠธ๋กœ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด๋ฏ€๋กœ O(l).
38+
- ๊ธธ์ด ๊ฐ’์„ splitํ•ด์„œ ๋ฆฌ์ŠคํŠธ๋กœ ๋งŒ๋“ ๋‹ค. O(n)
39+
- ์ข…ํ•ฉํ•˜๋ฉด O(l + n)์ธ๋ฐ, ์ด๋•Œ n์€ ๋ฌด์กฐ๊ฑด l ์ดํ•˜์ด๋ฏ€๋กœ O(l).
40+
41+
TC:
42+
- encode
43+
- n๊ฐœ์˜ ์•„์ดํ…œ์„ ์ˆœํšŒํ•˜๋ฉด์„œ ๊ธธ์ด๋ฅผ ์–ป๋Š”๋‹ค. O(n).
44+
- decode
45+
- ๊ธธ์ด ๊ฐ’์„ splitํ•ด์„œ ๋ฆฌ์ŠคํŠธ๋กœ ๋งŒ๋“ ๋‹ค. O(n).
46+
- ๋ˆ„์ ํ•ฉ์„ ํ™œ์šฉํ•˜์—ฌ ๊ธธ์ด ๊ฐ’ ๋ฆฌ์ŠคํŠธ๋ฅผ ํ•œ ๋ฒˆ ์ˆœํšŒ. O(n).
47+
- ์ข…ํ•ฉํ•˜๋ฉด O(n).
48+
"""
49+
50+
51+
class Solution:
52+
"""
53+
@param: strs: a list of strings
54+
@return: encodes a list of strings to a single string.
55+
"""
56+
57+
def encode(self, strs):
58+
body = "".join(strs)
59+
header = ",".join([str(len(i)) for i in strs])
60+
return header + ":" + body
61+
62+
"""
63+
@param: str: A string
64+
@return: decodes a single string to a list of strings
65+
"""
66+
67+
def decode(self, str):
68+
header, body = str.split(":", 1)
69+
len_list = [int(i) for i in header.split(",")]
70+
start_ind = 0
71+
result = []
72+
for i in len_list:
73+
result.append(body[start_ind : start_ind + i])
74+
start_ind += i
75+
return result

โ€Žvalid-anagram/haklee.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""TC: O(n), SC: O(n)
2+
3+
์—ฌ๊ธฐ์„œ n์€ s, t์˜ ๊ธธ์ด๊ฐ’ ์ค‘ ํฐ ๊ฒƒ์ด๋ผ ๊ฐ€์ •.
4+
5+
SC:
6+
- Counter๋Š” s, t์— ๋“ค์–ด์žˆ๋Š” ๊ธ€์ž๋“ค์„ key๋กœ ํ•˜๋Š” dict. ์ฆ‰, SC๋Š” O(n).
7+
8+
TC:
9+
- s, t์— ๋“ค์–ด์žˆ๋Š” ๊ธ€์ž๋“ค์„ key๋กœ dict๋ฅผ ์—…๋ฐ์ดํŠธ๋ฅผ ํ•œ ๋ฒˆ ํ• ๋•Œ O(1).
10+
- ์œ„์˜ ๊ณผ์ •์„ ๊ธธ์ด n๋งŒํผ ๋ฐ˜๋ณตํ•˜๋ฏ€๋กœ O(n).
11+
"""
12+
13+
from collections import Counter
14+
15+
16+
class Solution:
17+
def isAnagram(self, s: str, t: str) -> bool:
18+
# return sorted(s) == sorted(t) # TC: O(n log n), SC: O(n)
19+
return Counter(s) == Counter(t) # TC: O(n), SC: O(n)

0 commit comments

Comments
ย (0)