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

[BEMELON][WEEK 01](Python) 리트코드 문제 풀이 #300

Merged
merged 7 commits into from
Aug 16, 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
10 changes: 10 additions & 0 deletions contains-duplicate/bemelon.py
Copy link
Contributor

@TonyKim9401 TonyKim9401 Aug 11, 2024

Choose a reason for hiding this comment

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

Set 활용이 잘 된것 같습니다!

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
# Time Complexity: O(n)
# Space Complexity: O(n)
seen = set()
for num in nums:
if num in seen:
return True
seen.add(num)
return False
26 changes: 26 additions & 0 deletions kth-smallest-element-in-a-bst/bemelon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right

class Solution:
"""
Time Complexity: O(n)
Space Complexity: O(k)
"""

def inOrder(self, root: TreeNode, asc_sorted_list: list[int], k: int) -> None:
if root.left:
self.inOrder(root.left, asc_sorted_list, k)

asc_sorted_list.append(root.val)

if len(asc_sorted_list) < k and root.right:
self.inOrder(root.right, asc_sorted_list, k)

def kthSmallest(self, root: TreeNode, k: int) -> int:
asc_sorted_list = []
self.inOrder(root, asc_sorted_list, k)

return asc_sorted_list[k - 1]
18 changes: 18 additions & 0 deletions number-of-1-bits/bemelon.py
Copy link
Contributor

Choose a reason for hiding this comment

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

재귀는 생각하지 못했는데, bit 연산을 최적화하여 잘 사용하신것 같습니다!

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution:
def hammingWeight_v1(self, n: int) -> int:
# Recursive solution
# Time complexity: O(log n)
# Space complexity: O(log n)
if n == 0: return 0
elif n % 2 == 0: return self.hammingWeight(n // 2)
else: return self.hammingWeight(n // 2) + 1

def hammingWeight(self, n: int) -> int:
# Iterative solution
# Time complexity: O(log n)
# Space complexity: O(1)
set_bit_cnt = 0
while n > 0:
set_bit_cnt += n & 1
n >>= 1
return set_bit_cnt
49 changes: 49 additions & 0 deletions palindromic-substrings/bemelon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
class Solution:
def countSubstrings_v1(self, s: str) -> int:
def isPalindrome(self, substr: str) -> bool:
return len(substr) <= 1 or (substr[0] == substr[-1] and self.isPalindrome(substr[1:-1]))

# Brute-Force Solution - TLE
count = 0
for l in range(1, len(s) + 1):
for start in range(0, len(s)):
if start + l > len(s): continue

substr = s[start: start + l]
if (self.isPalindrome(substr)):
count += 1
return count

def countSubstrings(self, s: str) -> int:
"""
Dynamic Programming Solution
Time Complexity: O(N^2)
Space Complexity: O(N^2)
"""
n = len(s)

# isPalindrome[i][j] => Palindrome at s[i:j]?
isPalindrome = [[False] * n for _ in range(n)]
answer = 0
# 1. "a", "b", "c" are all Palindrome
for i in range(n):
isPalindrome[i][i] = True
answer += 1

# 2. "a{x}" are Palindrome if a == {x}
for i in range(n - 1):
if s[i] == s[i + 1]:
isPalindrome[i][i + 1] = True
answer += 1

# 3. else) str[i:j] is Palindrome if str[i + 1: j - 1] ... is Palinedrome
for size in range(3, n + 1):
for start in range(n - size + 1):
end = start + size - 1

if s[start] == s[end] and isPalindrome[start + 1][end - 1]:
isPalindrome[start][end] = True
answer += 1

return answer

22 changes: 22 additions & 0 deletions top-k-frequent-elements/bemelon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from collections import defaultdict

class Solution:
def topKFrequent(self, nums: list[int], k: int) -> list[int]:
"""
Time complexity: O(nlogn)
Space complexity: O(n)
"""
if len(nums) == k:
return list(set(nums))

num_cnt = defaultdict(int)
for num in nums:
num_cnt[num] += 1

return list(
sorted(
num_cnt,
key=lambda x: num_cnt[x],
reverse=True
)
)[:k]