Skip to content

Commit 4eda421

Browse files
authored
Merge pull request #303 from HaJunYoo/main
[HaJunYoo][WEEK 01] 리트코드 문제 풀이
2 parents cafbe96 + b9ae750 commit 4eda421

File tree

5 files changed

+85
-0
lines changed

5 files changed

+85
-0
lines changed

contains-duplicate/hajunyoo.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
# Time complexity: O(n)
3+
def containsDuplicate(self, nums: List[int]) -> bool:
4+
string_len = len(nums)
5+
set_len = len(set(nums))
6+
7+
return string_len != set_len
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
from collections import defaultdict
8+
9+
10+
class Solution:
11+
# Time complexity: O(n)
12+
def kthSmallest(self, root: Optional[TreeNode], k: int) -> int:
13+
self.count = 0
14+
self.result = 0
15+
16+
def dfs(node):
17+
if not node:
18+
return
19+
20+
dfs(node.left)
21+
22+
self.count += 1
23+
if self.count == k:
24+
self.result = node.val
25+
return
26+
27+
dfs(node.right)
28+
29+
dfs(root)
30+
return self.result

number-of-1-bits/hajunyoo.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
# Time complexity: O(log n)
3+
def hammingWeight(self, n: int) -> int:
4+
cnt = 0
5+
while n > 0:
6+
if n % 2 == 1:
7+
cnt += 1
8+
n = n // 2
9+
return cnt

palindromic-substrings/hajunyoo.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
# Time complexity: O(n^2) = O(n) * O(n)
3+
def countSubstrings(self, s: str) -> int:
4+
self.count = 0
5+
n = len(s)
6+
7+
def two_pointer_expand(left, right):
8+
while left >= 0 and right < n and s[left] == s[right]:
9+
self.count += 1
10+
left -= 1
11+
right += 1
12+
13+
for i in range(0, n):
14+
# 1, 3, 5 ...
15+
two_pointer_expand(i, i)
16+
# 2, 4, 6 ...
17+
two_pointer_expand(i, i + 1)
18+
19+
return self.count

top-k-frequent-elements/hajunyoo.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from collections import defaultdict
2+
from typing import List
3+
4+
5+
class Solution:
6+
# Time complexity: O(nlogn) -> O(n) + O(nlogn) + O(k)
7+
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
8+
counter_dict = defaultdict(int)
9+
10+
for n in nums:
11+
counter_dict[n] += 1
12+
13+
count_list = []
14+
for key, val in counter_dict.items():
15+
count_list.append((key, val))
16+
17+
count_list.sort(key=lambda x: x[1], reverse=True)
18+
answer = [a for a, b in count_list[:k]]
19+
20+
return answer

0 commit comments

Comments
 (0)