-
-
Notifications
You must be signed in to change notification settings - Fork 361
[Yiseull] WEEK 02 solutions #2682
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
4a9f95d
f173778
23487fb
82f4521
1659154
9f3f0b1
f87bd6d
cb9db2b
4f952d7
8893b18
351c777
ba521a8
497e96c
6bafe9b
4e4e371
6c17af8
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,27 @@ | ||
| class Solution: | ||
| def threeSum(self, nums: list[int]) -> list[list[int]]: | ||
| answer = set() | ||
|
|
||
| nums = sorted(nums) | ||
|
|
||
| n = len(nums) | ||
| for i in range(n - 2): | ||
| if nums[i] > 0: | ||
| break | ||
|
|
||
| if i > 0 and nums[i] == nums[i - 1]: | ||
| continue | ||
|
|
||
| left, right = i + 1, n - 1 | ||
| while left < right: | ||
| threeSum = nums[i] + nums[left] + nums[right] | ||
| if threeSum < 0: | ||
| left += 1 | ||
| elif threeSum == 0: | ||
| answer.add((nums[i], nums[left], nums[right])) | ||
| left += 1 | ||
| right -= 1 | ||
| else: | ||
| right -= 1 | ||
|
|
||
| return list(answer) |
|
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. 공간 복잡도를 O(1)으로 최적화할 수 있을 것 같습니다!
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,11 @@ | ||
| class Solution: | ||
| def climbStairs(self, n: int) -> int: | ||
| if n == 1: return 1 | ||
|
|
||
| dp = [0 for _ in range(n + 1)] | ||
| dp[0], dp[1] = 1, 1 | ||
|
|
||
| for i in range(2, n + 1): | ||
| dp[i] = dp[i - 1] + dp[i - 2] | ||
|
|
||
| return dp[n] |
|
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. answer[0]을 1로 할당하니까 로직이 잘 읽히네요. 잘 봤습니다. 공간/시간 복잡도를 코드에 적어주시면 좋을 것 같아요!
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,15 @@ | ||
| class Solution: | ||
| def productExceptSelf(self, nums: List[int]) -> List[int]: | ||
| n = len(nums) | ||
| answer = [1] | ||
|
|
||
| # answer[i] -> nums[i] 왼쪽 값들의 곱 | ||
| for i in range(1, n): | ||
| answer.append(answer[i - 1] * nums[i - 1]) | ||
|
|
||
| tmp = 1 | ||
| for i in range(n - 1, -1, -1): | ||
|
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. 개인적으로 저는 이렇게 작성하는거를 선호합니다! for i in reversed(range(n)): |
||
| answer[i] *= tmp | ||
| tmp *= nums[i] | ||
|
|
||
| return answer | ||
|
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. 파이썬의 언어적인 특성을 잘 활용하신 것 같습니다! 저는 성능에 도움이 될 것 같아서, 아래와 같이 길이가 다를 경우 얼리리턴을 해주었습니다. 참고 부탁드립니다~ if len(s) != len(t):
return False
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: Counter를 사용해 간결하고 정확하게 비교. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| from collections import Counter | ||
|
|
||
| class Solution: | ||
| def isAnagram(self, s: str, t: str) -> bool: | ||
| return Counter(s) == Counter(t) |
|
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,27 @@ | ||
| /** | ||
| * Definition for a binary tree node. | ||
| * public class TreeNode { | ||
| * int val; | ||
| * TreeNode left; | ||
| * TreeNode right; | ||
| * TreeNode() {} | ||
| * TreeNode(int val) { this.val = val; } | ||
| * TreeNode(int val, TreeNode left, TreeNode right) { | ||
| * this.val = val; | ||
| * this.left = left; | ||
| * this.right = right; | ||
| * } | ||
| * } | ||
| */ | ||
| class Solution { | ||
| public boolean isValidBST(TreeNode root) { | ||
| return validate(root, null, null); | ||
| } | ||
|
|
||
| private boolean validate(TreeNode node, Integer low, Integer high) { | ||
| if (node == null) return true; | ||
| if (low != null && low >= node.val) return false; | ||
| if (high != null && high <= node.val) return false; | ||
| return validate(node.left, low, node.val) && validate(node.right, node.val, high); | ||
| } | ||
| } |
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.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 정렬 후 중복 제거를 위한 체크와 투 포인터 이동으로 불필요 연산을 피함. 결과를 집합으로 모아 중복 제거.
개선 제안: 현재 구현이 적절해 보입니다.