Skip to content

Commit dcb60f8

Browse files
authored
[yuseok89] WEEK02 solutions (#2686)
* contains duplicate solution * two sum solution * top k frequent elements solution * longest consecutive sequence solution * house robber solution * add newline * 리뷰 반영 * 리뷰 반영 * 개선 - AI assist * valid anagram solution * climbing stairs solution * product of array except self solution * 3sum solution * validate binary search tree solution * 공간복잡도 업데이트 * 공간복잡도 업데이트
1 parent 3fd1331 commit dcb60f8

5 files changed

Lines changed: 109 additions & 0 deletions

File tree

3sum/yuseok89.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# TC: O(N^2)
2+
# SC: O(N)
3+
class Solution:
4+
def threeSum(self, nums: list[int]) -> list[list[int]]:
5+
6+
cnt_dict = Counter(nums)
7+
nums_uniq = sorted(cnt_dict.keys())
8+
n = len(nums_uniq)
9+
10+
ret = []
11+
12+
for num in cnt_dict.keys():
13+
if num == 0:
14+
if cnt_dict[num] >= 3:
15+
ret.append([0, 0, 0])
16+
elif cnt_dict[num] >= 2 and num * 2 * -1 in cnt_dict:
17+
ret.append([num, num, num * 2 * -1])
18+
19+
for idx1, num1 in enumerate(nums_uniq):
20+
if num1 > 0:
21+
break
22+
23+
for idx2 in range(idx1 + 1, n):
24+
num2 = nums_uniq[idx2]
25+
num3 = (num1 + num2) * -1
26+
27+
if num2 >= num3:
28+
break
29+
elif num3 in cnt_dict:
30+
ret.append([num1, num2, num3])
31+
32+
return ret
33+

climbing-stairs/yuseok89.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# TC: O(N)
2+
# SC: O(1)
3+
class Solution:
4+
def climbStairs(self, n: int) -> int:
5+
6+
one_down = 1
7+
two_down = 0
8+
9+
for _ in range(1, n):
10+
cur = two_down + one_down
11+
two_down = one_down
12+
one_down = cur
13+
14+
return two_down + one_down
15+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# TC: O(N)
2+
# SC: O(N)
3+
class Solution:
4+
def productExceptSelf(self, nums: List[int]) -> List[int]:
5+
6+
n = len(nums)
7+
prefix_prod = [0] * n
8+
suffix_prod = [0] * n
9+
10+
prefix_prod[0] = nums[0]
11+
suffix_prod[-1] = nums[-1]
12+
13+
for idx in range(1, n):
14+
prefix_prod[idx] = prefix_prod[idx - 1] * nums[idx]
15+
suffix_prod[-idx - 1] = suffix_prod[-idx] * nums[-idx - 1]
16+
17+
ret = []
18+
19+
ret.append(suffix_prod[1])
20+
for idx in range(1, n - 1):
21+
ret.append(prefix_prod[idx - 1] * suffix_prod[idx + 1])
22+
ret.append(prefix_prod[n - 2])
23+
24+
return ret
25+

valid-anagram/yuseok89.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# TC: O(N)
2+
# SC: O(1)
3+
class Solution:
4+
def isAnagram(self, s: str, t: str) -> bool:
5+
6+
if len(s) != len(t):
7+
return False
8+
9+
cnt_s = Counter(s)
10+
cnt_t = Counter(t)
11+
12+
return cnt_s == cnt_t
13+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# TC: O(N)
2+
# SC: O(H)
3+
# Definition for a binary tree node.
4+
# class TreeNode:
5+
# def __init__(self, val=0, left=None, right=None):
6+
# self.val = val
7+
# self.left = left
8+
# self.right = right
9+
class Solution:
10+
11+
def isValid(self, left_bound, right_bound, root):
12+
if root is None:
13+
return True
14+
if left_bound is not None and left_bound >= root.val:
15+
return False
16+
if right_bound is not None and right_bound <= root.val:
17+
return False
18+
19+
return self.isValid(left_bound, root.val, root.left) and self.isValid(root.val, right_bound, root.right)
20+
21+
def isValidBST(self, root: Optional[TreeNode]) -> bool:
22+
return self.isValid(None, root.val, root.left) and self.isValid(root.val, None, root.right)
23+

0 commit comments

Comments
 (0)