-
-
Notifications
You must be signed in to change notification settings - Fork 362
[parkhojeong] WEEK 02 Solutions #2701
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
Changes from all commits
3b6b3ea
efee3c7
45ac2b3
7059ab1
f5ad25e
f1fa20e
543e06e
a2e3989
4ed105c
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,31 @@ | ||
| class Solution: | ||
| def threeSum(self, nums: list[int]) -> list[list[int]]: | ||
| result_arr = [] | ||
| n = len(nums) | ||
| nums.sort() | ||
|
|
||
| for i in range(n - 2): | ||
| if nums[i] > 0: | ||
| break | ||
| if i > 0 and nums[i] == nums[i - 1]: | ||
| continue | ||
|
|
||
| j = i + 1 | ||
| k = n - 1 | ||
| while j < k: | ||
| total = nums[i] + nums[j] + nums[k] | ||
|
|
||
| if total == 0: | ||
| result_arr.append([nums[i], nums[j], nums[k]]) | ||
| while j < k and nums[j] == nums[j + 1]: | ||
| j += 1 | ||
| while j < k and nums[k] == nums[k - 1]: | ||
| k -= 1 | ||
| j += 1 | ||
| k -= 1 | ||
| elif total > 0: | ||
| k -= 1 | ||
| else: | ||
| j += 1 | ||
|
|
||
| return result_arr |
|
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)으로 줄일 수 있습니다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| class Solution: | ||
| def climbStairs(self, n: int) -> int: | ||
| if n == 1: | ||
| return 1 | ||
|
|
||
| arr = [0] * n | ||
| arr[0], arr[1] = 1, 2 | ||
|
|
||
| for i in range(2, n): | ||
| arr[i] = arr[i - 1] + arr[i - 2] | ||
|
|
||
| return arr[-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.
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,14 @@ | ||
| class Solution: | ||
| def productExceptSelf(self, nums: List[int]) -> List[int]: | ||
| n = len(nums) | ||
| answer = [1] * n | ||
|
|
||
| for i in range(1, n): | ||
| answer[i] = answer[i -1] * nums[i - 1] | ||
|
|
||
| R = 1 | ||
| for i in range(n - 1, -1, -1): | ||
| answer[i] *= R | ||
| R *= 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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 각 노드에 대해 상한/하한을 전달해 BST 특성을 검사합니다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| # 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: | ||
| return self.dfs(root, ~sys.maxsize, sys.maxsize) | ||
|
|
||
| def dfs(self, root, min_val, max_val) -> bool: | ||
| val = root.val | ||
| if val <= min_val or val >= max_val: | ||
| return False | ||
|
|
||
| if root.left: | ||
| if not self.dfs(root.left, min_val, val): | ||
| return False | ||
| if root.right: | ||
| if not self.dfs(root.right, val, max_val): | ||
| return False | ||
|
|
||
| return True | ||
|
|
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.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 정렬과 중복 제거를 통해 불필요한 계산을 피하고, 두 포인터의 이동으로 전체 시간 복잡도를 O(n^2)로 달성합니다.
개선 제안: 현재 구현이 적절해 보입니다.