-
-
Notifications
You must be signed in to change notification settings - Fork 195
[kayden] Week 01 Solutions #327
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
Changes from 6 commits
a2f5247
d842c9f
0066475
c14ea01
64758f0
534fb76
83fcb5b
f23c237
0faaeae
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,6 @@ | ||
# 시간복잡도: O(N) | ||
# 공간복잡도: O(N) | ||
class Solution: | ||
def containsDuplicate(self, nums: List[int]) -> bool: | ||
unique_nums = set(nums) | ||
return len(unique_nums) != len(nums) | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# 시간복잡도: O(N) | ||
# 공간복잡도: O(1) | ||
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: | ||
self.result = 0 | ||
self.count = 0 | ||
|
||
def dfs(node): | ||
if node is None: | ||
return | ||
|
||
dfs(node.left) | ||
|
||
self.count += 1 | ||
|
||
if self.count == k: | ||
self.result = node.val | ||
return | ||
|
||
dfs(node.right) | ||
|
||
dfs(root) | ||
|
||
return self.result |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 쉽고 간결한 풀이네요 👍 다만 해당 문제의 목적 자체는 이진연산을 활용하는 것이기에, 시간이 나신다면 다른 방법으로도 풀어서 시간복잡도를 줄여보는것도 좋을것같습니다! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 리뷰 해주셔서 감사합니다! 이진연산으로 풀어볼 생각은 못해봤네요! 내일 모임 전에 한번 시도해보겠습니다! |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# 시간복잡도: O(log N) | ||
# 공간복잡도: O(log N) | ||
class Solution: | ||
def hammingWeight(self, n: int) -> int: | ||
return bin(n).count("1") |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# 시간복잡도: O(N^2) | ||
# 공간복잡도: O(N^2) | ||
class Solution: | ||
def countSubstrings(self, s: str) -> int: | ||
n = len(s) | ||
count = n | ||
isPalindrome = [[False for _ in range(n)] for _ in range(n)] | ||
|
||
for i in range(n): | ||
isPalindrome[i][i] = True | ||
if i < n-1 and s[i] == s[i+1]: | ||
isPalindrome[i][i+1] = True | ||
count += 1 | ||
|
||
for length in range(3, n + 1): | ||
for i in range(n - length + 1): | ||
j = i + length - 1 | ||
if isPalindrome[i+1][j-1] and s[i] == s[j]: | ||
isPalindrome[i][j] = True | ||
count += 1 | ||
|
||
return count |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# 시간복잡도: O(Nlog N) | ||
# 공간복잡도: O(N) | ||
from collections import Counter | ||
|
||
class Solution: | ||
def topKFrequent(self, nums: List[int], k: int) -> List[int]: | ||
return [num for num, count in Counter(nums).most_common(k)] |
Uh oh!
There was an error while loading. Please reload this page.
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.
간결함을 원하신다면 unique_nums를 그냥 return 다음으로 옮겨버리는거도 나쁘지 않아보이네요 :)
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.
한줄로 작성한게 훨씬 깔끔하네요!