Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[haklee] week 2 #342

Merged
merged 1 commit into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hash table์„ ์‚ฌ์šฉํ•ด์„œ ๋…ธ๋“œ์˜ ์ˆœ์„œ๋กœ ์ •๋ ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๊ตฐ์š”๐Ÿ‘


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)
leokim0922 marked this conversation as resolved.
Show resolved Hide resolved

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))
35 changes: 35 additions & 0 deletions counting-bits/haklee.py
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๋Š”
Comment on lines +11 to +12
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋ฌธ์ œ๋ฅผ ์ •ํ™•ํžˆ ์ดํ•ดํ•˜๊ณ  ์žˆ๋‹ค๊ณ  ๋Š๊ปด์ง€๋Š” testcase์ธ๋ฐ์š”? ๋„ˆ๋ฌด ์ข‹์Šต๋‹ˆ๋‹ค :)

[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]
32 changes: 32 additions & 0 deletions decode-ways/haklee.py
leokim0922 marked this conversation as resolved.
Show resolved Hide resolved
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)
Copy link
Contributor

@leokim0922 leokim0922 Aug 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์—ฌ๊ธฐ์„œ y ๊ฐ’์„ ๊ตณ์ด int๋กœ ํ•œ๋ฒˆ๋” ์บ์ŠคํŒ… ํ•ด์ฃผ์‹ ๊ฑฐ๋Š” ํ™•์‹คํ•˜๊ฒŒ ๋งŒ๋“ค๊ณ  ๊ฐ€์‹œ๋ ค๊ณ  ํ•œ๊ฑธ๊นŒ์š”? ๊ถ๊ธˆํ•ด์„œ ํ•œ ๋ฒˆ ๋Œ๋ ค๋ณด๊ณ  ์™”๋Š”๋ฐ boolean์œผ๋กœ ๋ฐ˜ํ™˜์‹œ์ผœ๋„ ํŒŒ์ด์ฌ ๋‚ด๋ถ€์—์„œ๋Š” int๋กœ ์ฒ˜๋ฆฌํ•˜๋Š”๊ฑฐ ๊ฐ™์•„์„œ์š” ๐Ÿ˜„

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์•ž์„œ ์„ค๋ช…ํ•œ ์ด์œ ๋กœ int๋กœ ๋งŒ๋“ค์ง€ ์•Š์•„๋„ ์ฝ”๋“œ๋Š” ์ž˜ ๋Œ์•˜์„ํ…๋ฐ, ์ค‘๊ฐ„์— ๋””๋ฒ„๊น… ํ•˜๋ ค๊ณ  x, y๊ฐ’์„ ์ถœ๋ ฅํ•ด๋ณด๋‹ค๊ฐ€ ์ˆซ์ž๋“ค ์‚ฌ์ด์— True๊ฐ€ ๋ฐ•ํ˜€์žˆ๋Š” ๊ฒƒ์„ ๋ณด๊ณ  ๋งค์šฐ ๋ถ„๋…ธํ•˜์—ฌ ๊ณง์žฅ int๋กœ ๋งŒ๋“ค์–ด๋ฒ„๋ ธ์Šต๋‹ˆ๋‹ค...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋งค์šฐ ๋””ํ…Œ์ผํ•œ ์„ค๋ช… ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค! ์‚ฌ์‹ค ์ž‘์„ฑํ•˜์‹  ์ฝ”๋“œ์™€ ๋‚ด์šฉ ๋ชจ๋‘ ์ œ๊ฐ€ ์ง€ํ–ฅํ•˜๋Š” ๋ฐฉํ–ฅ์ด๋ž‘ ๋น„์Šทํ•ด์„œ ์—ฌ์ญค๋ดค์–ด์š” ๐Ÿ˜„ '์ ์ ˆํ•œ ์งฌ๊ณผ ์‹ค๋ ฅ์ด ์Œ“์ด๋ฉด ์ด๋ ‡๊ฒŒ ์งค์ˆ˜๋„ ์žˆ๊ตฌ๋‚˜' ์ƒ๊ฐ์ด ๋“œ๋Š” ์†”๋ฃจ์…˜๋“ค์ด์—ˆ์Šต๋‹ˆ๋‹ค. ์•ž์œผ๋กœ๋„ ์ž˜ ๋ถ€ํƒ๋“œ๋ฆด๊ฒŒ์š”!

# 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))
leokim0922 marked this conversation as resolved.
Show resolved Hide resolved
return y
75 changes: 75 additions & 0 deletions encode-and-decode-strings/haklee.py
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
Comment on lines +52 to +75
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ด ์†”๋ฃจ์…˜ ๋„ˆ๋ฌด ์•„๋ฆ„๋‹ต์Šต๋‹ˆ๋‹ค! ๐Ÿ˜ ํ˜น์‹œ ๋‹ค์Œ ๋ชจ์ž„ ๋•Œ ๋‹ค๋ฅธ ๋ฉค๋ฒ„๋“คํ•œํ…Œ ์„ค๋ช… ์ข€ ํ•ด์ฃผ์‹ค ์ˆ˜ ์žˆ์œผ์‹ค๊นŒ์š”?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์˜ค์˜ค ๋„ต ์ข‹์Šต๋‹ˆ๋‹ค!

19 changes: 19 additions & 0 deletions valid-anagram/haklee.py
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ํŒŒ์ด์ฌ์—์„œ๋Š” ํ…Œ์ด๋ธ”(๋”•์…”๋„ˆ๋ฆฌ)์„ ๋น„๊ตํ•˜๋Š” ๋ฉ”์„œ๋“œ๊ฐ€ ์กด์žฌํ•˜๋Š”๊ตฐ์š”