Skip to content

Commit cd3eb3f

Browse files
authored
Merge pull request #2048 from doh6077/main
[doh6077] WEEK 02 solutions
2 parents 7bc5b0a + fe26eb0 commit cd3eb3f

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from typing import List
2+
3+
class Solution:
4+
def threeSum(self, nums: List[int]) -> List[List[int]]:
5+
nums.sort()
6+
n = len(nums)
7+
res: List[List[int]] = []
8+
9+
for i in range(n - 2):
10+
# i 쀑볡 μŠ€ν‚΅
11+
if i > 0 and nums[i] == nums[i - 1]:
12+
continue
13+
14+
# nums[i] μ΄ν›„λŠ” μ „λΆ€ μ–‘μˆ˜ β†’ 더 이상 0 λͺ» λ§Œλ“¦
15+
if nums[i] > 0:
16+
break
17+
18+
left, right = i + 1, n - 1
19+
20+
while left < right:
21+
total = nums[i] + nums[left] + nums[right]
22+
23+
if total == 0:
24+
res.append([nums[i], nums[left], nums[right]])
25+
26+
left += 1
27+
right -= 1
28+
29+
# left 쀑볡 μŠ€ν‚΅
30+
while left < right and nums[left] == nums[left - 1]:
31+
left += 1
32+
33+
# right 쀑볡 μŠ€ν‚΅
34+
while left < right and nums[right] == nums[right + 1]:
35+
right -= 1
36+
37+
elif total < 0:
38+
left += 1
39+
else:
40+
right -= 1
41+
42+
return res
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
# dictionary use
3+
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
4+
result = {} # key: μ›μ†Œ, value: λ“±μž₯ 횟수
5+
for n in nums:
6+
if n in result:
7+
result[n] = result[n] + 1
8+
else:
9+
result[n] = 1
10+
11+
# κ°€μž₯ 자주 λ“±μž₯ν•œ μ›μ†Œ k개 λ°˜ν™˜
12+
return sorted(result.keys(), key=lambda x: result[x], reverse=True)[:k]
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def isValidBST(self, root: Optional[TreeNode]) -> bool:
3+
def validate(node: Optional[TreeNode], low: float, high: float) -> bool:
4+
if not node:
5+
return True
6+
if not (low < node.val < high):
7+
return False
8+
return (validate(node.left, low, node.val) and
9+
validate(node.right, node.val, high))
10+
return validate(root, float('-inf'), float('inf'))

0 commit comments

Comments
Β (0)