Skip to content

Commit 1a6b0f1

Browse files
authored
Merge pull request #300 from BEMELON/main
[BEMELON][WEEK 01](Python) 리트코드 문제 풀이
2 parents 80129c6 + 4341deb commit 1a6b0f1

File tree

5 files changed

+125
-0
lines changed

5 files changed

+125
-0
lines changed

contains-duplicate/bemelon.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def containsDuplicate(self, nums: List[int]) -> bool:
3+
# Time Complexity: O(n)
4+
# Space Complexity: O(n)
5+
seen = set()
6+
for num in nums:
7+
if num in seen:
8+
return True
9+
seen.add(num)
10+
return False
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class TreeNode:
2+
def __init__(self, val=0, left=None, right=None):
3+
self.val = val
4+
self.left = left
5+
self.right = right
6+
7+
class Solution:
8+
"""
9+
Time Complexity: O(n)
10+
Space Complexity: O(k)
11+
"""
12+
13+
def inOrder(self, root: TreeNode, asc_sorted_list: list[int], k: int) -> None:
14+
if root.left:
15+
self.inOrder(root.left, asc_sorted_list, k)
16+
17+
asc_sorted_list.append(root.val)
18+
19+
if len(asc_sorted_list) < k and root.right:
20+
self.inOrder(root.right, asc_sorted_list, k)
21+
22+
def kthSmallest(self, root: TreeNode, k: int) -> int:
23+
asc_sorted_list = []
24+
self.inOrder(root, asc_sorted_list, k)
25+
26+
return asc_sorted_list[k - 1]

number-of-1-bits/bemelon.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def hammingWeight_v1(self, n: int) -> int:
3+
# Recursive solution
4+
# Time complexity: O(log n)
5+
# Space complexity: O(log n)
6+
if n == 0: return 0
7+
elif n % 2 == 0: return self.hammingWeight(n // 2)
8+
else: return self.hammingWeight(n // 2) + 1
9+
10+
def hammingWeight(self, n: int) -> int:
11+
# Iterative solution
12+
# Time complexity: O(log n)
13+
# Space complexity: O(1)
14+
set_bit_cnt = 0
15+
while n > 0:
16+
set_bit_cnt += n & 1
17+
n >>= 1
18+
return set_bit_cnt

palindromic-substrings/bemelon.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
class Solution:
2+
def countSubstrings_v1(self, s: str) -> int:
3+
def isPalindrome(self, substr: str) -> bool:
4+
return len(substr) <= 1 or (substr[0] == substr[-1] and self.isPalindrome(substr[1:-1]))
5+
6+
# Brute-Force Solution - TLE
7+
count = 0
8+
for l in range(1, len(s) + 1):
9+
for start in range(0, len(s)):
10+
if start + l > len(s): continue
11+
12+
substr = s[start: start + l]
13+
if (self.isPalindrome(substr)):
14+
count += 1
15+
return count
16+
17+
def countSubstrings(self, s: str) -> int:
18+
"""
19+
Dynamic Programming Solution
20+
Time Complexity: O(N^2)
21+
Space Complexity: O(N^2)
22+
"""
23+
n = len(s)
24+
25+
# isPalindrome[i][j] => Palindrome at s[i:j]?
26+
isPalindrome = [[False] * n for _ in range(n)]
27+
answer = 0
28+
# 1. "a", "b", "c" are all Palindrome
29+
for i in range(n):
30+
isPalindrome[i][i] = True
31+
answer += 1
32+
33+
# 2. "a{x}" are Palindrome if a == {x}
34+
for i in range(n - 1):
35+
if s[i] == s[i + 1]:
36+
isPalindrome[i][i + 1] = True
37+
answer += 1
38+
39+
# 3. else) str[i:j] is Palindrome if str[i + 1: j - 1] ... is Palinedrome
40+
for size in range(3, n + 1):
41+
for start in range(n - size + 1):
42+
end = start + size - 1
43+
44+
if s[start] == s[end] and isPalindrome[start + 1][end - 1]:
45+
isPalindrome[start][end] = True
46+
answer += 1
47+
48+
return answer
49+

top-k-frequent-elements/bemelon.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from collections import defaultdict
2+
3+
class Solution:
4+
def topKFrequent(self, nums: list[int], k: int) -> list[int]:
5+
"""
6+
Time complexity: O(nlogn)
7+
Space complexity: O(n)
8+
"""
9+
if len(nums) == k:
10+
return list(set(nums))
11+
12+
num_cnt = defaultdict(int)
13+
for num in nums:
14+
num_cnt[num] += 1
15+
16+
return list(
17+
sorted(
18+
num_cnt,
19+
key=lambda x: num_cnt[x],
20+
reverse=True
21+
)
22+
)[:k]

0 commit comments

Comments
 (0)