-
-
Notifications
You must be signed in to change notification settings - Fork 195
[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
Merged
Merged
[hak.lee] week 1 #305
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,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)) |
This file contains hidden or 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,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) |
This file contains hidden or 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,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)]) |
This file contains hidden or 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,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 |
This file contains hidden or 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,26 @@ | ||
"""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 | ||
ref: https://docs.python.org/3/library/collections.html#collections.Counter | ||
A Counter is a dict subclass for counting hashable objects. | ||
|
||
Counter, ์ฆ, dict์ (๊ฐ, ํค) ํํ์ sortingํ๋ ๋ฐ์ TC: O(n log n), SC: O(n) | ||
* ์ด๋ ๊ฐ์ด ํด์๋ก ์์ ์ค๊ฒ ํ๊ธฐ ์ํด ๊ฐ์ ์์๋ก ๋ฐ๊ฟ์คฌ๋ค. | ||
|
||
3 | ||
sorted๋ ๋ฆฌ์คํธ์์ ํํ์ ๋ ๋ฒ์งธ ์์ดํ ๋ง ๋ฝ์ ๋ค์์ | ||
๋ฆฌ์คํธ์ ์ k๊ฐ์ ์์ดํ ๋ฆฌํด. | ||
""" | ||
|
||
from collections import Counter | ||
|
||
|
||
class Solution: | ||
def topKFrequent(self, nums: List[int], n: int) -> List[int]: | ||
return [i for _, i in sorted([(-v, k) for k, v in Counter(nums).items()])][:n] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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)
๋ผ๊ณ ํ์๋๋ผ๋ ๋ฉด์ ๊ด๊ณผ ํฌ๊ฒ ๋ ผ์์ ์ฌ์ง๋ ์์ ๊ฒ ๊ฐ์ต๋๋ค.