-
-
Notifications
You must be signed in to change notification settings - Fork 363
[yuseok89] WEEK 03 Solutions #2719
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
8b4a01f
4b09054
da3aad9
5c4d5fb
03f3f1a
d8d5ea4
1f003e2
a022d10
b8248c4
51c1012
65431c0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
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,28 @@ | ||
| # TC: O(N^T) | ||
| # SC: O(T) | ||
| class Solution: | ||
|
|
||
| def rec(self, cur_idx, target, candi, cur_combi, ans): | ||
|
|
||
| if target == 0: | ||
| ans.append(cur_combi.copy()) | ||
|
|
||
| if cur_idx == len(candi): | ||
| return | ||
|
|
||
| for idx in range(cur_idx, len(candi)): | ||
| if target < candi[idx]: | ||
| continue | ||
|
|
||
| cur_combi.append(candi[idx]) | ||
| self.rec(idx, target - candi[idx], candi, cur_combi, ans) | ||
| cur_combi.pop() | ||
|
|
||
| def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: | ||
|
|
||
| ans = [] | ||
|
|
||
| self.rec(0, target, candidates, [], ans) | ||
|
|
||
| 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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
풀이 1:
|
| 복잡도 | |
|---|---|
| Time | O(n) |
| Space | O(n) |
피드백: 상태 수를 줄여 중복 계산을 제거했지만, 재귀 대신 배열 기반 반복으로도 구현 가능하다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 2: Solution.rec — Time: O(n) / Space: O(n)
| 복잡도 | |
|---|---|
| Time | O(n) |
| Space | O(n) |
피드백: 입력 길이에 선형적으로 의존하는 동적계획으로 구성되어 있다.
개선 제안: 현재 구현이 적절해 보입니다.
💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| # TC: O(N) | ||
| # SC: O(N) | ||
| class Solution: | ||
|
|
||
| memo = [] | ||
|
|
||
| def rec(self, idx, s, n): | ||
| if self.memo[idx] == -1: | ||
|
|
||
| cnt = 0 | ||
|
|
||
| if int(s[idx]) != 0: | ||
| cnt += self.rec(idx + 1, s, n) | ||
|
|
||
| if idx + 1 < n: | ||
| if int(s[idx]) != 0 and int(s[idx]) * 10 + int(s[idx + 1]) <= 26: | ||
| cnt += self.rec(idx + 2, s, n) | ||
|
|
||
| self.memo[idx] = cnt | ||
|
|
||
| return self.memo[idx] | ||
|
|
||
| def numDecodings(self, s: str) -> int: | ||
|
|
||
| self.memo = [-1] * (len(s) + 1) | ||
| self.memo[len(s)] = 1 | ||
|
|
||
| return self.rec(0, s, len(s)) | ||
|
|
|
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
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.
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
풀이 1:
|
| 유저 분석 | 실제 분석 | 결과 | |
|---|---|---|---|
| Time | O(N) | O(n) | ✅ |
| Space | O(1) | O(1) | ✅ |
피드백: 4줄짜리 간결한 구현으로 최적의 시간/공간 복잡도를 달성한다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 2: Solution.maxSubArray (주석 대안) — Time: O(n) / Space: O(1)
| 복잡도 | |
|---|---|
| Time | O(n) |
| Space | O(1) |
피드백: 주석은 실행에 영향 없이 대안을 보여준다.
개선 제안: 현재 구현이 적절해 보입니다.
💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| # TC: O(N) | ||
| # SC: O(1) | ||
| class Solution: | ||
| def maxSubArray(self, nums: List[int]) -> int: | ||
| sum = nums[0] | ||
| ans = sum | ||
|
|
||
| for num in nums[1:]: | ||
| sum = max(num, sum + num) | ||
| ans = max(ans, sum) | ||
|
|
||
| return ans | ||
|
|
||
| # class Solution: | ||
| # def maxSubArray(self, nums: List[int]) -> int: | ||
| # sum = nums[0] if nums[0] < 0 else 0 | ||
| # ans = sum | ||
|
|
||
| # for num in nums: | ||
| # sum = max(num, sum + num) | ||
| # ans = max(ans, sum) | ||
|
|
||
| # 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. 깔끔하게 잘 해결하신거 같아요!
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,13 @@ | ||
| # TC: O(logN) | ||
| # SC: O(1) | ||
| class Solution: | ||
| def hammingWeight(self, n: int) -> int: | ||
|
|
||
| cnt = 0 | ||
|
|
||
| while n > 0: | ||
| cnt = cnt + (n % 2) | ||
| n //= 2 | ||
|
|
||
| return cnt | ||
|
|
|
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. 해당 풀이는 Immutable 한 string을 하나 더 만들기 때문에 O(N)의 공간복잡도를 가질거에요!
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. 네 맞네요. 일단 이 문제는 투포인터로 접근하는게 맞을 것 같습니다.
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,43 @@ | ||
| # TC: O(N) | ||
| # SC: O(1) | ||
| class Solution: | ||
|
|
||
| def is_alphanum(self, c): | ||
| return 'a' <= c <= 'z' or '0' <= c <= '9' | ||
|
|
||
| def isPalindrome(self, s: str) -> bool: | ||
|
|
||
| left = 0 | ||
| right = len(s) - 1 | ||
|
|
||
| s = s.lower() | ||
|
|
||
| while(left < right): | ||
| while left < len(s) and not self.is_alphanum(s[left]): | ||
| left += 1 | ||
| while right >= 0 and not self.is_alphanum(s[right]): | ||
| right -= 1 | ||
|
|
||
| if left < right and s[left] != s[right]: | ||
| return False | ||
|
|
||
| left += 1 | ||
| right -= 1 | ||
|
|
||
| return True | ||
|
|
||
| # class Solution: | ||
| # def isPalindrome(self, s: str) -> bool: | ||
| # | ||
| # import re | ||
| # | ||
| # s = re.sub(r'[^a-zA-Z0-9]', '', s.lower()) | ||
| # | ||
| # n = len(s) | ||
| # | ||
| # for idx in range(0, n // 2): | ||
| # if s[idx] != s[n - idx - 1]: | ||
| # 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.
다 좋으신데, 백트래킹 부분이 너무 복잡한거 같아요
별도의 함수로 제외하지 않고 구현하시면 훨씬 보기 좋게 될거 같아요
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.
백트래킹 코드를 좀 더 보기좋게 수정했습니다.
별도의 함수로 빼지 않고 구현은 어떤 방식을 말하시는걸까요?