-
-
Notifications
You must be signed in to change notification settings - Fork 360
[yuseok89] WEEK02 solutions #2686
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
4836151
cdc00da
cc0a988
c894055
4e4e412
32cafb9
49ecb59
a6fcc33
38a1f67
8bd5a32
05685ed
cd23e34
120df1f
1d4ff54
23142a7
0cb14ca
9465142
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,33 @@ | ||
| # TC: O(N^2) | ||
| # SC: O(N) | ||
| class Solution: | ||
| def threeSum(self, nums: list[int]) -> list[list[int]]: | ||
|
|
||
| cnt_dict = Counter(nums) | ||
| nums_uniq = sorted(cnt_dict.keys()) | ||
| n = len(nums_uniq) | ||
|
|
||
| ret = [] | ||
|
|
||
| for num in cnt_dict.keys(): | ||
| if num == 0: | ||
| if cnt_dict[num] >= 3: | ||
| ret.append([0, 0, 0]) | ||
| elif cnt_dict[num] >= 2 and num * 2 * -1 in cnt_dict: | ||
| ret.append([num, num, num * 2 * -1]) | ||
|
|
||
| for idx1, num1 in enumerate(nums_uniq): | ||
| if num1 > 0: | ||
| break | ||
|
|
||
| for idx2 in range(idx1 + 1, n): | ||
| num2 = nums_uniq[idx2] | ||
| num3 = (num1 + num2) * -1 | ||
|
|
||
| if num2 >= num3: | ||
| break | ||
| elif num3 in cnt_dict: | ||
| ret.append([num1, num2, num3]) | ||
|
|
||
| return ret | ||
|
|
|
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 @@ | ||
| # TC: O(N) | ||
| # SC: O(1) | ||
| class Solution: | ||
| def climbStairs(self, n: int) -> int: | ||
|
|
||
| one_down = 1 | ||
| two_down = 0 | ||
|
|
||
| for _ in range(1, n): | ||
| cur = two_down + one_down | ||
| two_down = one_down | ||
| one_down = cur | ||
|
|
||
| return two_down + one_down | ||
|
|
|
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,25 @@ | ||
| # TC: O(N) | ||
| # SC: O(N) | ||
| class Solution: | ||
| def productExceptSelf(self, nums: List[int]) -> List[int]: | ||
|
|
||
| n = len(nums) | ||
| prefix_prod = [0] * n | ||
| suffix_prod = [0] * n | ||
|
|
||
| prefix_prod[0] = nums[0] | ||
| suffix_prod[-1] = nums[-1] | ||
|
|
||
| for idx in range(1, n): | ||
| prefix_prod[idx] = prefix_prod[idx - 1] * nums[idx] | ||
| suffix_prod[-idx - 1] = suffix_prod[-idx] * nums[-idx - 1] | ||
|
|
||
| ret = [] | ||
|
|
||
| ret.append(suffix_prod[1]) | ||
| for idx in range(1, n - 1): | ||
| ret.append(prefix_prod[idx - 1] * suffix_prod[idx + 1]) | ||
| ret.append(prefix_prod[n - 2]) | ||
|
|
||
| return ret | ||
|
|
|
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,13 @@ | ||
| # TC: O(N) | ||
| # SC: O(1) | ||
| class Solution: | ||
| def isAnagram(self, s: str, t: str) -> bool: | ||
|
|
||
| if len(s) != len(t): | ||
| return False | ||
|
|
||
| cnt_s = Counter(s) | ||
| cnt_t = Counter(t) | ||
|
|
||
| return cnt_s == cnt_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,23 @@ | ||
| # TC: O(N) | ||
| # SC: O(H) | ||
| # 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 isValid(self, left_bound, right_bound, root): | ||
| if root is None: | ||
| return True | ||
| if left_bound is not None and left_bound >= root.val: | ||
| return False | ||
| if right_bound is not None and right_bound <= root.val: | ||
| return False | ||
|
|
||
| return self.isValid(left_bound, root.val, root.left) and self.isValid(root.val, right_bound, root.right) | ||
|
|
||
| def isValidBST(self, root: Optional[TreeNode]) -> bool: | ||
| return self.isValid(None, root.val, root.left) and self.isValid(root.val, None, root.right) | ||
|
|
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.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 중복 조합을 피하기 위한 정렬과 두 포인터 탐색 로직이 들어 있습니다. 해시맵을 쓰지 않더라도 시간 복잡도는 두 포인터 탐색으로 달성됩니다.
개선 제안: 현재 구현이 적절해 보입니다.