-
-
Notifications
You must be signed in to change notification settings - Fork 363
[alphaorderly] WEEK 03 Solutions #2706
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
4d5f428
3ac63f5
2536209
612dbbb
a7c2098
79f6f86
3855235
33ab6bc
b1de0e1
f9bf16c
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 @@ | ||
|
|
||
| """ | ||
| Time Complexity: O(N^target) | ||
| Space Complexity: O(target) | ||
|
|
||
| Classic backtracking approach. | ||
| - Use a helper function to backtrack and generate all possible combinations. | ||
| """ | ||
| # class Solution: | ||
| # def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: | ||
| # N = len(candidates) | ||
| # candidates.sort() | ||
|
|
||
| # ans = [] | ||
| # app = [] | ||
|
|
||
| # def backtracking(last: int, left: int) -> None: | ||
| # if left == 0: | ||
| # ans.append(app[:]) | ||
| # return | ||
|
|
||
| # for i in range(last, N): | ||
| # if candidates[i] > left: | ||
| # continue | ||
|
|
||
| # app.append(candidates[i]) | ||
| # backtracking(i, left - candidates[i]) | ||
| # app.pop() | ||
|
|
||
| # backtracking(0, target) | ||
|
|
||
| # return ans | ||
|
|
||
| """ | ||
| Time Complexity: O(N * target) | ||
| Space Complexity: O(target) | ||
|
|
||
| - Use a dynamic programming approach to store the combinations that sum to each target. | ||
| - dp[j] is a list of lists that sum to j. | ||
| """ | ||
| class Solution: | ||
| def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: | ||
| dp = [list() for _ in range(target + 1)] | ||
| dp[0] = [[]] | ||
|
|
||
| for c in candidates: | ||
| for j in range(c, target + 1): | ||
| for partial in dp[j - c]: | ||
| dp[j].append(partial + [c]) | ||
|
|
||
| return dp[-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,31 @@ | ||
| """ | ||
| Time Complexity: O(N) | ||
| Space Complexity: O(1) | ||
|
|
||
| prev_2 : number of ways to decode the string ending with the previous two characters | ||
| prev_1 : number of ways to decode the string ending with the previous character | ||
| new_prev : number of ways to decode the string ending with the current character | ||
| """ | ||
| class Solution: | ||
| def numDecodings(self, s: str) -> int: | ||
| N = len(s) | ||
|
|
||
| if s[0] == "0": | ||
| return 0 | ||
|
|
||
| if N == 1: | ||
| return 1 | ||
|
|
||
| prev_2 = 1 | ||
| prev_1 = int(1 <= int(s[0] + s[1]) <= 26) + int(s[1] != "0") | ||
|
|
||
| for i in range(2, N): | ||
| new_prev = 0 | ||
| if 1 <= int(s[i - 1] + s[i]) <= 26 and s[i - 1] != "0": | ||
| new_prev += prev_2 | ||
| if s[i] != "0": | ||
| new_prev += prev_1 | ||
|
|
||
| prev_2, prev_1 = prev_1, new_prev | ||
|
|
||
| return prev_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,71 @@ | ||
|
|
||
|
|
||
| """ | ||
| Time Complexity: O(NLogN) | ||
| Space Complexity: O(N) ( Reason: Recursive stack pointer for every element ) | ||
|
|
||
| Divide and Conquer Approach | ||
|
|
||
| 1. Divide the array into two halves | ||
| 2. Find the maximum subarray sum in the left half | ||
| 3. Find the maximum subarray sum in the right half | ||
| 4. Find the maximum subarray sum that crosses the midpoint | ||
| 5. Return the maximum of the three sums | ||
| """ | ||
| # class Solution: | ||
| # def maxSubArray(self, nums: List[int]) -> int: | ||
|
|
||
| # def divide(start: int, end: int) -> int: | ||
| # if start >= end: | ||
| # return nums[start] | ||
|
|
||
| # mid = (start + end) // 2 | ||
|
|
||
| # left = divide(start, mid) | ||
| # right = divide(mid + 1, end) | ||
|
|
||
| # left_largest = -float('inf') | ||
| # right_largest = -float('inf') | ||
|
|
||
| # left_acc = 0 | ||
| # right_acc = 0 | ||
|
|
||
| # left_index = mid | ||
| # right_index = mid + 1 | ||
|
|
||
| # while left_index >= start: | ||
| # left_acc += nums[left_index] | ||
| # left_largest = max(left_largest, left_acc) | ||
| # left_index -= 1 | ||
|
|
||
| # while right_index <= end: | ||
| # right_acc += nums[right_index] | ||
| # right_largest = max(right_largest, right_acc) | ||
| # right_index += 1 | ||
|
|
||
| # return max(left_largest + right_largest, left, right) | ||
|
|
||
| # return divide(0, len(nums) - 1) | ||
|
|
||
|
|
||
|
|
||
|
|
||
| """ | ||
| Time Complexity: O(N) | ||
| Space Complexity: O(1) | ||
|
|
||
| prev : maximum sum of the subarray ending with the previous element | ||
| ans : maximum sum of the subarray | ||
| curr : maximum sum of the subarray ending with the current element | ||
| """ | ||
| class Solution: | ||
| def maxSubArray(self, nums: List[int]) -> int: | ||
| N = len(nums) | ||
| prev = nums[0] | ||
| ans = nums[0] | ||
|
|
||
| for i in range(1, N): | ||
| prev = max(nums[i], prev + nums[i]) | ||
| ans = max(ans, prev) | ||
|
|
||
| return ans |
|
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,8 @@ | ||
| class Solution: | ||
| def hammingWeight(self, n: int) -> int: | ||
| ans = 0 | ||
| while n: | ||
| ans += n & 1 | ||
| n >>= 1 | ||
| return ans | ||
|
|
|
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,6 @@ | ||
| class Solution: | ||
| def isPalindrome(self, s: str) -> bool: | ||
| processed = "".join(ch.lower() for ch in s if ch.isalnum()) | ||
|
|
||
| return processed == processed[::-1] | ||
|
|
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.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
풀이 1:
Solution.combinationSum— Time: O(n * target) / Space: O(target)피드백: 목적 합이 target에 도달하도록 각 합에 대한 부분 해결책들을 저장하고, 이를 이용해 최종 결과를 구성한다.
개선 제안: 현재 구현이 효과적입니다.
풀이 2:
Solution.combinationSum— Time: O(N^target) / Space: O(target)피드백: 깊은 탐색으로 모든 조합을 시도하므로 지수적 시간 복잡도가 발생할 수 있다.
개선 제안: 현재 구현이 적절해 보입니다.