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
33 changes: 33 additions & 0 deletions 3sum/sangbeenmoon.py

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🏷️ 알고리즘 패턴 분석

  • 패턴: Two Pointers, Hash Map / Hash Set
  • 설명: 삼합(3Sum) 문제에서 정렬 후 양 끝 포인터를 이동하는 방식으로 탐색하므로 Two Pointers 패턴이 적용됩니다. 또한 중복 제거를 위해 Hash Map/Hash Set를 활용해 결과 중복을 방지하는 부분이 있습니다.

📊 시간/공간 복잡도 분석

유저 분석 실제 분석 결과
Time O(n^2) O(n^2)
Space O(n) O(1)

피드백: 정렬 후 외부 루프와 내부 투 포인터로 복잡도를 달성했다. 다만 중복 제거를 visited로 추가했는데, 전체적으로 불필요한 해시맵 사용 가능성이 있다. 두 번째 구현처럼 중복 제거를 더 간결하게 처리하는 편이 좋다.

개선 제안: 고려해볼 만한 대안: 중복 제거를 리스트 스킵 방식으로 처리하고 해시맵 없이도 중복 없는 결과를 얻도록 구현을 단순화

Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,36 @@ def threeSum(self, nums: list[int]) -> list[list[int]]:
left = left + 1
right = right - 1
return answer


# TC : O(n^2)
# SC : O(n)

class Solution:
def threeSum(self, nums: list[int]) -> list[list[int]]:

nums.sort()

answer_map = {}
answer = []

for i in range(len(nums)):
target = -1 * nums[i]

left = i + 1
right = len(nums) - 1
Comment on lines +46 to +50

@parkhojeong parkhojeong Jul 4, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

정렬이 된 상태라 nums[i] 와 nums[i - 1] 이 같은 경우는 스킵해주고 nums[i] > 0 이 된 상태에서는 break 해주면 최적화가 가능할 거 같습니다 :)

ex. [ -2, -1, -1, -1, 0, 1, 2, 3] , -1이 한번인 경우만 실행하고 이후는 스킵, nums[i]가 0보다 커지면 양수 + 양수 + 양수 이기 때문에 종료


while left < right:
if nums[left] + nums[right] == target:
candidate = (target * -1, nums[left], nums[right])
if candidate not in answer_map:
answer_map[candidate] = True
answer.append([target * -1, nums[left], nums[right]])
Comment on lines +52 to +57

@parkhojeong parkhojeong Jul 4, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

해시맵을 사용해서 candidate를 담아서 중복을 체크하셨군요!
이미 정렬이 된 상태라서 nums[left] == nums[left+1] 인 케이스를 스킵해주면(right도 동일) candidate를 사용하지 않고 not in 로직을 안타기 때문에 더 최적화가 가능할 거 같아요!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

좀 더 깔끔하게 중복 체크할 수 있는 방법이 있었네요. 짚어주셔서 감사합니다

left += 1
Comment on lines +55 to +58

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

성공한 경우 right -= 1 도 함께 해주면 좋을 거 같아요!

right -= 1
elif nums[left] + nums[right] < target:
left += 1
else:
right -= 1
return answer

35 changes: 35 additions & 0 deletions validate-binary-search-tree/sangbeenmoon.py

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🏷️ 알고리즘 패턴 분석

  • 패턴: Depth-First Search, Binary Search, Hash Map / Hash Set
  • 설명: 첫 번째 코드는 재귀로 BST의 각 노드를 탐색하며 자식 노드가 특정 범위 안에 있는지 확인한다. 두 가지 구현 모두 트리 구조의 각 노드를 방문하는 DFS 패턴이며, 자식 노드의 값 범위를 제한해 BST 특성을 검사하는 방식이다. 두 번째 구현은 min/max 범위 추적을 통한 Divide and Conquer식 DFS로 BST 여부를 판단한다.

📊 시간/공간 복잡도 분석

복잡도
Time O(n)
Space O(h)

피드백: 두 가지 구현이 섞여 있는데, 첫 번째 구현은 전역 상태를 이용해 재귀적으로 범위를 검사한다. 두 번째는 재귀적으로 범위를 넘겨주는 일반적인 패턴으로 안정적이다.

개선 제안: 현재 구현이 적절해 보입니다.

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,38 @@ def go_right(self, cur:TreeNode, mm:int, MM:int):
if cur.right:
self.go_right(cur.right, cur.val, MM)




# ---------


# Definition for a binary tree node.

# TC: O(n)
# SC: O(h)

# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def isValidBST(self, root: Optional[TreeNode]) -> bool:

def isValid(min_val, max_val, cur: TreeNode) -> bool:


if cur is None:
return True

if min_val is not None and cur.val <= min_val:
return False

if max_val is not None and cur.val >= max_val:
return False

return isValid(min_val, cur.val, cur.left) and isValid(cur.val, max_val, cur.right)

return isValid(None, None ,root)

Loading