-
-
Notifications
You must be signed in to change notification settings - Fork 245
[hak.lee] week 1 #305
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
[hak.lee] week 1 #305
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
"""TC: O(n)? O(n^2)?, SC: O(n) | ||
|
||
ref: https://wiki.python.org/moin/TimeComplexity | ||
set๋ dict์ ๊ฑฐ์ ๋น์ทํ๊ฒ ๊ตฌํ๋์ด ์๋๋ฐ, | ||
dict์ `Set Item`์ Average Case๋ O(1)์ด๋๋ผ๋ | ||
Amortized Worst Case๊ฐ O(n)์ด๋ค! | ||
|
||
์ฆ, set์ ์์ดํ ์ ์ถ๊ฐํ ๋๋ง๋ค ํด์ ์ถฉ๋์ด ์ผ์ด๋ ๊ฒฝ์ฐ | ||
์ต์ ์ ๊ฒฝ์ฐ O(n^2)์ด ๊ฑธ๋ฆฌ๋ฏ๋ก, ์๋์ set(nums)์ | ||
TC๊ฐ O(n^2)์ด ๋๋ ๊ฒ์ผ๊น..? | ||
|
||
set(nums)์ ๊ฒฐ๊ณผ๊ฐ ์ต์ ์ ๊ฒฝ์ฐ SC๊ฐ O(n)์ด๋ค. | ||
""" | ||
|
||
|
||
class Solution: | ||
def containsDuplicate(self, nums: List[int]) -> bool: | ||
return len(nums) != len(set(nums)) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
"""TC: O(n), SC: O(n) | ||
BST์ ์ ์ฒด ๋ ธ๋ ๊ฐ์๊ฐ n๊ฐ๋ผ๊ณ ๊ฐ์ . | ||
TC๋ ์ต์ ์ ๊ฒฝ์ฐ BST ๋ ธ๋ ์ ์ฒด๋ฅผ ์ํํด์ผ ํ๋ฏ๋ก O(n). | ||
๋ ธ๋๋ฅผ ์ํํ๋ฉด์ ์ง๋์จ ๋ ธ๋ ๊ฐ์ ๋ฐ๋ก ์ ์ฅํ์ง ์๊ณ ์นด์ดํฐ๋ก ์ฒ๋ฆฌํ๊ณ ์๋ค. | ||
์ฆ, traverse ํจ์๋ฅผ ์ฌ๊ท์ ์ผ๋ก ํธ์ถํ๋ ๋ถ๋ถ์ ์ฝ ์คํ์ ๊น์ด ๊ฐ๋ง ๊ณ ๋ คํ๋ฉด ๋๋๋ฐ, | ||
root ๋ ธ๋๋ถํฐ ์ ๋ถ left๋ก ์ด์ด์ง ํธ๋ฆฌ๊ฐ ๋ง๋ค์ด์ง ๊ฒฝ์ฐ SC๊ฐ 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 kthSmallest(self, root: Optional[TreeNode], k: int) -> int: | ||
global cnt | ||
cnt = 0 | ||
|
||
def traverse(node: Optional[TreeNode]) -> int: | ||
"""์์ด๋์ด: | ||
๋ ธ๋์ ์ผ์ชฝ ์์์ ๋จผ์ ์ํํ๊ณ , | ||
๋ณธ์ธ์ ๊ฐ์ ์ฒดํฌํ๋ฉด์ cnt๊ฐ์ ๋๋ฆฌ๊ณ , | ||
๋ ธ๋์ ์ค๋ฅธ์ชฝ ์์์ ์ํ. | ||
์ํํ๋ค๊ฐ cnt๊ฐ์ด k๊ฐ ๋๋ฉด, ์ฆ, k๋ฒ์งธ ์์ ์์๋ฅผ ์ฐพ์ผ๋ฉด | ||
์ํ๋ฅผ ๋ฉ์ถ๊ณ ์ฐพ์ ๊ฐ์ ๋ฆฌํด. | ||
๋ฆฌํด๋ ๊ฐ์ด -1์ธ ๊ฒฝ์ฐ ์ํ ์ค ์ํ๋ ๊ฐ์ ์ฐพ์ง ๋ชปํ๋ค๋ ๋ป์ด๋ค. | ||
๋ฌธ์ ์กฐ๊ฑด์์ ๋ ธ๋์ val ๊ฐ์ด 0 ์ด์์ด๋ผ๊ณ ๋์ด์์ด์ -1์ ์ ํ. | ||
""" | ||
global cnt | ||
return -1 if not node\ | ||
else v if (v := traverse(node.left)) >= 0\ | ||
else node.val if (cnt := cnt + 1) == k\ | ||
else traverse(node.right) | ||
|
||
return traverse(root) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
"""TC: O(log n), SC: O(log n) | ||
|
||
n์ด 2๋ฐฐ ์ปค์ง๋ฉด bin(n)์ ๊ธธ์ด๊ฐ 1 ๋์ด๋๊ณ , ์ด bin(n) ๊ฐ์ | ||
์ ๋ถ ์ํํ๋ฏ๋ก O(log n). | ||
bin(n)์ ๋ฆฌ์คํธ๋ก ๋ฐ๊ฟ์ sum์ ํ์ผ๋ฏ๋ก SC๋ O(log n)์ด ๋๋ค. | ||
|
||
ํ์ง๋ง n์ ํฌ๊ธฐ๊ฐ 2^31 - 1 ์ดํ๋ก ์ ํ๋์ด ์์ผ๋ฏ๋ก | ||
TC, SC ๋ถ์์ด ํฌ๊ฒ ์๋ฏธ์์ง ์์. | ||
""" | ||
|
||
|
||
class Solution: | ||
def hammingWeight(self, n: int) -> int: | ||
return sum([i == "1" for i in bin(n)]) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
"""TC: O(n^2), SC: O(1) | ||
SC๋ sol์ด๋ผ๋ ์นด์ดํฐ ๊ฐ๋ง ๊ด๋ฆฌํ๊ณ ์์ผ๋ฏ๋ก O(1). | ||
TC๋ | ||
- ํฐ๋ฆฐ๋๋กฌ์ ์ค์ฌ์ ์ ์์น๊ฐ range(l)๋ก ์ฃผ์ด์ง๋ฏ๋ก ์ฌ๊ธฐ์ O(n), | ||
- ์ค์ฌ์ ์์ ์์๊ณผ ๋์ ํ ์นธ์ฉ ๋๋ ค๊ฐ๋ฉด์ ํฐ๋ฆฐ๋๋กฌ ์ฒดํฌํ ๋ O(1), | ||
- ์์๊ณผ ๋์ด ์ต๋๋ก ๋์ด๋ ์ ์๋ ๊ธธ์ด๊ฐ O(n), | ||
์ฆ, O(n^2). | ||
""" | ||
|
||
|
||
class Solution: | ||
def countSubstrings(self, x: str) -> int: | ||
l = len(x) | ||
sol = 0 | ||
|
||
for i in range(l): | ||
# ์์๊ณผ ๋์ด ์ผ์นํ๋ ์ํ์์ ์์. | ||
# ์ฆ, ํฐ๋ฆฐ๋๋กฌ์ ๊ธธ์ด๊ฐ ํ์. | ||
s = e = i | ||
b = True | ||
while b: | ||
sol += (b := (0 <= s and e < l and x[s] == x[e])) | ||
s -= 1 | ||
e += 1 | ||
|
||
# ์์๊ณผ ๋์ด ํ๋ ์ฐจ์ด๋๋ ์ํ์์ ์์. | ||
# ์ฆ, ํฐ๋ฆฐ๋๋กฌ์ ๊ธธ์ด๊ฐ ์ง์. | ||
s = e = i | ||
e += 1 | ||
b = True | ||
while b: | ||
sol += (b := (0 <= s and e < l and x[s] == x[e])) | ||
s -= 1 | ||
e += 1 | ||
|
||
return sol |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
"""TC: O(n log n), SC: O(n) | ||
|
||
1 | ||
nums๋ฅผ ์ํํ๋ฉด์ dict๋ฅผ ์ฑ์ฐ๋ ๋ฐ์ TC: O(n), SC: O(n) | ||
* ๋จ, dict์์ `Set Item` ํ ๋ | ||
Amortized Worst Case O(n) ๋ง๊ณ | ||
Average Case O(1) ์ ์ฉ ๊ฐ์ | ||
|
||
2 | ||
dict์ (๊ฐ, ํค) ํํ์ sortingํ๋ ๋ฐ์ TC: O(n log n), SC: O(n) | ||
* ์ด๋ ๊ฐ์ด ํด์๋ก ์์ ์ค๊ฒ ํ๊ธฐ ์ํด ๊ฐ์ ์์๋ก ๋ฐ๊ฟ์คฌ๋ค. | ||
|
||
3 | ||
sorted๋ ๋ฆฌ์คํธ์์ ํํ์ ๋ ๋ฒ์งธ ์์ดํ ๋ง ๋ฝ์ ๋ค์์ | ||
๋ฆฌ์คํธ์ ์ k๊ฐ์ ์์ดํ ๋ฆฌํด. | ||
""" | ||
|
||
|
||
class Solution: | ||
def topKFrequent(self, nums: List[int], n: int) -> List[int]: | ||
d = {} | ||
[d.update({i: d.get(i, 0) + 1}) for i in nums] | ||
|
||
return [i for _, i in sorted([(-v, k) for k, v in d.items()])][:n] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ผ๋ฐ์ ์ผ๋ก ์ฝ๋ฉ ํ ์คํธ์์๋ ์ด ์ ๋๊น์ง ๊น์ ๋ณต์ก๋ ๋ถ์์ ์ํ์ง๋ ์์ต๋๋ค. ๋๋ถ๋ถ์ ๊ฐ๋ฐ์๋ค์ ํด์ ์ถฉ๋์ด ๊ฑฐ์ ์ผ์ด๋์ง ์๋๋ค๋ ๊ฒ์ ๊ฐ์ ํ๊ณ ํด์ ํ ์ด๋ธ ๊ธฐ๋ฐ์ ์๋ฃ ๊ตฌ์กฐ๋ฅผ ์ฌ์ฉํฉ๋๋ค. ๋ฐ๋ผ์ ์ด ํ์ด์ ์๊ฐ ๋ณต์ก๋๋
O(n)
๋ผ๊ณ ํ์๋๋ผ๋ ๋ฉด์ ๊ด๊ณผ ํฌ๊ฒ ๋ ผ์์ ์ฌ์ง๋ ์์ ๊ฒ ๊ฐ์ต๋๋ค.