Skip to content

Commit c620f71

Browse files
sangbeenmoonsangbeenmoon
andauthored
[sangbeenmoon] WEEK 02 Solutions (#2704)
* solved same-tree. * solved top-k-frequent-elements. * remove outdated files. * LCS, house-robber. * week2. --------- Co-authored-by: sangbeenmoon <moon.sb@navercorp.com>
1 parent a2c4129 commit c620f71

2 files changed

Lines changed: 68 additions & 0 deletions

File tree

3sum/sangbeenmoon.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,36 @@ def threeSum(self, nums: list[int]) -> list[list[int]]:
3030
left = left + 1
3131
right = right - 1
3232
return answer
33+
34+
35+
# TC : O(n^2)
36+
# SC : O(n)
37+
38+
class Solution:
39+
def threeSum(self, nums: list[int]) -> list[list[int]]:
40+
41+
nums.sort()
42+
43+
answer_map = {}
44+
answer = []
45+
46+
for i in range(len(nums)):
47+
target = -1 * nums[i]
48+
49+
left = i + 1
50+
right = len(nums) - 1
51+
52+
while left < right:
53+
if nums[left] + nums[right] == target:
54+
candidate = (target * -1, nums[left], nums[right])
55+
if candidate not in answer_map:
56+
answer_map[candidate] = True
57+
answer.append([target * -1, nums[left], nums[right]])
58+
left += 1
59+
right -= 1
60+
elif nums[left] + nums[right] < target:
61+
left += 1
62+
else:
63+
right -= 1
64+
return answer
65+

validate-binary-search-tree/sangbeenmoon.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,38 @@ def go_right(self, cur:TreeNode, mm:int, MM:int):
3838
if cur.right:
3939
self.go_right(cur.right, cur.val, MM)
4040

41+
42+
43+
44+
# ---------
45+
46+
47+
# Definition for a binary tree node.
48+
49+
# TC: O(n)
50+
# SC: O(h)
51+
52+
# class TreeNode:
53+
# def __init__(self, val=0, left=None, right=None):
54+
# self.val = val
55+
# self.left = left
56+
# self.right = right
57+
class Solution:
58+
def isValidBST(self, root: Optional[TreeNode]) -> bool:
59+
60+
def isValid(min_val, max_val, cur: TreeNode) -> bool:
61+
62+
63+
if cur is None:
64+
return True
65+
66+
if min_val is not None and cur.val <= min_val:
67+
return False
68+
69+
if max_val is not None and cur.val >= max_val:
70+
return False
71+
72+
return isValid(min_val, cur.val, cur.left) and isValid(cur.val, max_val, cur.right)
73+
74+
return isValid(None, None ,root)
75+

0 commit comments

Comments
 (0)