-
-
Notifications
You must be signed in to change notification settings - Fork 361
[daehyun99] WEEK02 solutions #2687
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| class Solution: | ||
| def threeSum(self, nums: List[int]) -> List[List[int]]: | ||
| res = [] | ||
| nums.sort() | ||
|
|
||
| for i, a in enumerate(nums): | ||
| if a > 0: | ||
| break | ||
| if i > 0 and a == nums[i-1]: | ||
| continue | ||
|
|
||
| l, r = i+1, len(nums) - 1 | ||
| while l < r: | ||
| total = a + nums[l] + nums[r] | ||
| if total > 0: | ||
| r -= 1 | ||
| elif total < 0: | ||
| l += 1 | ||
| else: | ||
| res.append([a, nums[l], nums[r]]) | ||
| l += 1 | ||
| r -= 1 | ||
| while nums[l] == nums[l - 1] and l < r: | ||
| l += 1 | ||
| return res | ||
|
|
||
| """from collections import Counter | ||
|
|
||
| class Solution: | ||
| def threeSum(self, nums: list[int]) -> list[list[int]]: | ||
| counts = Counter(nums) | ||
| result: set[tuple[int, int, int]] = set() | ||
|
|
||
| for i in range(len(nums) - 2): | ||
| for j in range(i + 1, len(nums) - 1): | ||
| adding = -(nums[i] + nums[j]) | ||
|
|
||
| if adding not in counts: | ||
| continue | ||
|
|
||
| local_counts = Counter([nums[i], nums[j], adding]) | ||
|
|
||
| for num, count in local_counts.items(): | ||
| if counts[num] < count: | ||
| break | ||
| else: | ||
| triplet = tuple(sorted([nums[i], nums[j], adding])) | ||
| result.add(triplet) | ||
|
|
||
| return [list(triplet) for triplet in result] | ||
| """ |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 각 단계의 경우의 수를 상태로 유지해 빠르게 누적하는 방식으로 구현되었으나, 현재 구현은 직관적이지 않을 수 있다. 개선 제안: 속도와 가독성을 위해 간단한 피보나치 DP로 구현하거나, 공간을 더 절약하는 형태로 개선해 보세요.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| class Solution: | ||
| def climbStairs(self, n: int) -> int: | ||
| one_count = n | ||
| two_count = 0 | ||
| total_count = 0 | ||
|
|
||
| while one_count >=0 and two_count >=0: | ||
| # 조합 | ||
| total = one_count + two_count | ||
|
|
||
| count = 1 | ||
| for i in range(two_count): | ||
| count *= total - i | ||
| for i in range(two_count, 0, -1): | ||
| count /= i | ||
| total_count += count | ||
|
|
||
| one_count -=2 | ||
| two_count +=1 | ||
| return int(total_count) |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 제로 처리를 명확하게 분리했고, 추가 배열 없이도 결과를 계산할 수 있는 구조다. 개선 제안: 특히 제로가 하나인 경우와 없을 때의 분기 로직을 간결하게 다듬으면 가독성이 향상될 수 있다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| class Solution: | ||
| def productExceptSelf(self, nums: List[int]) -> List[int]: | ||
| product = 1 | ||
| zero_pos = set() | ||
|
|
||
| for i in range(len(nums)): | ||
| num = nums[i] | ||
| if num != 0: | ||
| product *= num | ||
| else: | ||
| zero_pos.add(i) | ||
|
|
||
| if len(zero_pos) >= 2: | ||
| return [0 for num in nums] | ||
| elif len(zero_pos) == 1: | ||
| return [0 if i not in zero_pos else product for i in range(len(nums))] | ||
| else: | ||
| return [int(product / num) for num in nums] |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. collections 의 Counter가 기본적으로 defaultdict(int) 처럼 없는 키값에 대해 0을 보낸다는것을 알고 계실까요?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
@alphaorderly
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 카운트 차이를 이용해 한 번의 순회로 결과를 판정한다. 간단하고 빠른 방법이다. 개선 제안: 필요 없을 때는 defaultdict 사용 대신 배열 인덱스 기반 카운트로 더 빠르게 구현 가능.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| from collections import defaultdict | ||
| class Solution: | ||
| def isAnagram(self, s: str, t: str) -> bool: | ||
| count = defaultdict(int) | ||
|
|
||
| if len(s) != len(t): | ||
| return False | ||
|
|
||
| for s_, t_ in zip(s, t): | ||
| count[s_] += 1 | ||
| count[t_] -= 1 | ||
|
|
||
| for key, val in count.items(): | ||
| if val != 0: | ||
| return False | ||
| return True |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 좌우 자식 노드에 대해 상한/하한 값을 전달하는 방식으로 중복 포함 여부를 올바르게 판단한다. 개선 제안: 스택을 이용한 비재귀 구현으로 재귀 깊이를 제어할 수 있다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # Definition for a binary tree node. | ||
| # 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 valid(node, left, right): | ||
| if not node: | ||
| return True | ||
| if not (left < node.val < right): | ||
| return False | ||
|
|
||
| return ( | ||
| valid(node.left, left, node.val) and | ||
| valid(node.right, node.val, right) | ||
| ) | ||
| return valid(root, float("-inf"), float("inf")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 정렬과 두 포인터로 중복을 건너뛰는 방식으로 필요하지 않은 탐색을 제거했다. 단일 루프 내에서 좌우 포인터를 조정하므로 시간 복잡도는 이 문제의 최적에 가깝다.
개선 제안: 현재 구현이 적절해 보입니다.