Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions 3sum/doh6077.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from typing import List

class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
nums.sort()
n = len(nums)
res: List[List[int]] = []

for i in range(n - 2):
# i 중복 스킵
if i > 0 and nums[i] == nums[i - 1]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 조건문이 없으면 제출 테스트케이스 끝자락에서 오류가 발생하더라구요..
[0, 0, 0, 0 ...] 이런 상황에서 유용하게 쓰일 것 같습니다👍

continue

# nums[i] 이후는 전부 양수 → 더 이상 0 못 만듦
if nums[i] > 0:
break

left, right = i + 1, n - 1

while left < right:
total = nums[i] + nums[left] + nums[right]

if total == 0:
res.append([nums[i], nums[left], nums[right]])

left += 1
right -= 1

# left 중복 스킵
while left < right and nums[left] == nums[left - 1]:
left += 1

# right 중복 스킵
while left < right and nums[right] == nums[right + 1]:
right -= 1

elif total < 0:
left += 1
else:
right -= 1

return res
12 changes: 12 additions & 0 deletions top-k-frequent-elements/doh6077.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution:
# dictionary use
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
result = {} # key: 원소, value: 등장 횟수
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해시 테이블을 활용해서 횟수 계산 로직이 명확하고 가독성이 좋아 이해하기 쉬운 코드였습니다👍

for n in nums:
if n in result:
result[n] = result[n] + 1
else:
result[n] = 1

# 가장 자주 등장한 원소 k개 반환
return sorted(result.keys(), key=lambda x: result[x], reverse=True)[:k]
10 changes: 10 additions & 0 deletions validate-binary-search-tree/doh6077.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Solution:
def isValidBST(self, root: Optional[TreeNode]) -> bool:
def validate(node: Optional[TreeNode], low: float, high: float) -> bool:
if not node:
return True
if not (low < node.val < high):
return False
return (validate(node.left, low, node.val) and
validate(node.right, node.val, high))
return validate(root, float('-inf'), float('inf'))
Comment on lines +1 to +10
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

재귀적으로 잘 푸신 것 같습니다!