Skip to content

[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

Merged
merged 9 commits into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
6 changes: 6 additions & 0 deletions contains-duplicate/kayden.py
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)
Copy link
Contributor

@leokim0922 leokim0922 Aug 17, 2024

Choose a reason for hiding this comment

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

간결함을 원하신다면 unique_nums를 그냥 return 다음으로 옮겨버리는거도 나쁘지 않아보이네요 :)

return len(set(nums)) != len(nums)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

한줄로 작성한게 훨씬 깔끔하네요!

32 changes: 32 additions & 0 deletions kth-smallest-element-in-a-bst/kayden.py
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
5 changes: 5 additions & 0 deletions number-of-1-bits/kayden.py
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.

리뷰 해주셔서 감사합니다! 이진연산으로 풀어볼 생각은 못해봤네요! 내일 모임 전에 한번 시도해보겠습니다!

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")
22 changes: 22 additions & 0 deletions palindromic-substrings/kayden.py
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
7 changes: 7 additions & 0 deletions top-k-frequent-elements/kayden.py
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)]