Skip to content

Commit 912181e

Browse files
author
sangbeenmoon
committed
week2.
1 parent 667ffe0 commit 912181e

2 files changed

Lines changed: 67 additions & 0 deletions

File tree

3sum/sangbeenmoon.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,35 @@ 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+
elif nums[left] + nums[right] < target:
60+
left += 1
61+
else:
62+
right -= 1
63+
return answer
64+

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(mm, MM, cur: TreeNode) -> bool:
61+
62+
63+
if cur == None:
64+
return True
65+
66+
if mm != None and cur.val <= mm:
67+
return False
68+
69+
if MM != None and cur.val >= MM:
70+
return False
71+
72+
return isValid(mm, cur.val, cur.left) and isValid(cur.val, MM, cur.right)
73+
74+
return isValid(None, None ,root)
75+

0 commit comments

Comments
 (0)