Skip to content

Commit f02e639

Browse files
committed
week02_4_5_solution
1 parent 1f7be88 commit f02e639

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

β€Ž3sum/YOOHYOJEONG.pyβ€Ž

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# https://leetcode.com/problems/3sum
2+
3+
class Solution(object):
4+
def threeSum(self, nums):
5+
nums.sort()
6+
result = []
7+
8+
for i in range(len(nums) - 2):
9+
10+
if i > 0 and nums[i] == nums[i - 1]:
11+
continue
12+
13+
left = i + 1
14+
right = len(nums) - 1
15+
16+
while left < right:
17+
total = nums[i] + nums[left] + nums[right]
18+
19+
if total < 0:
20+
left += 1
21+
22+
elif total > 0:
23+
right -= 1
24+
25+
else:
26+
result.append([nums[i], nums[left], nums[right]])
27+
28+
while left < right and nums[left] == nums[left + 1]:
29+
left += 1
30+
31+
while left < right and nums[right] == nums[right - 1]:
32+
right -= 1
33+
34+
left += 1
35+
right -= 1
36+
37+
return result
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# https://leetcode.com/problems/validate-binary-search-tree/description/
2+
3+
# λ„μ €νžˆ μ•ˆν’€μ–΄μ„œ μ§€ν”Όν‹°μ˜ 도움을 λ°›μ•˜μŠ΅λ‹ˆλ‹€..TT
4+
# μƒˆλ‘œ μ•Œκ²Œ 된 κ°œλ…
5+
# 이진 νŠΈλ¦¬λŠ” ν•˜λ‚˜μ˜ λ…Έλ“œκ°€ μ΅œλŒ€ 두 개의 μžμ‹ λ…Έλ“œλ₯Ό κ°€μ§€λŠ” 트리 자료ꡬ쑰
6+
# 두 개의 포인터λ₯Ό κ°€μ§€κ³  있으며, λ£¨νŠΈμ—μ„œ μ‹œμž‘ν•΄μ„œ μ•„λž˜λ‘œ λ‚΄λ €κ°€λŠ” 계측 ꡬ쑰
7+
# Binary Search Tree(BST)λŠ” 이진 트리의 ν•œ μ’…λ₯˜λ‘œ μ–΄λ–€ λ…Έλ“œμ˜ μ™Όμͺ½ μ„œλΈŒνŠΈλ¦¬μ— μžˆλŠ” λͺ¨λ“  값은 ν•΄λ‹Ή λ…Έλ“œλ³΄λ‹€ μž‘κ³  였λ₯Έμͺ½ μ„œλΈŒνŠΈλ¦¬μ— μžˆλŠ” λͺ¨λ“  값은 ν•΄λ‹Ή λ…Έλ“œλ³΄λ‹€ 큼
8+
# 이 λ¬Έμ œλŠ” μ£Όμ–΄μ§„ νŠΈλ¦¬κ°€ 이 κ·œμΉ™μ„ λ§Œμ‘±ν•˜λŠ”μ§€ ν™•μΈν•˜λŠ” λ¬Έμ œμž„
9+
# 각 λ…Έλ“œκ°€ κ°€μ§ˆ 수 μžˆλŠ” ν—ˆμš© λ²”μœ„(min, max)λ₯Ό μœ μ§€ν•˜λ©΄μ„œ 검사해야 함
10+
class Solution:
11+
def isValidBST(self, root):
12+
13+
def dfs(node, low, high):
14+
if not node:
15+
return True
16+
17+
if node.val <= low or node.val >= high:
18+
return False
19+
20+
return (
21+
dfs(node.left, low, node.val) and
22+
dfs(node.right, node.val, high)
23+
)
24+
25+
return dfs(root, float("-inf"), float("inf"))

0 commit comments

Comments
Β (0)