-
-
Notifications
You must be signed in to change notification settings - Fork 358
[sangbeenmoon] WEEK 01 solutions #2667
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
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 |
|---|---|---|
|
|
@@ -14,3 +14,29 @@ def rob(self, nums: List[int]) -> int: | |
| dp[i] = max(dp[i-2] + nums[i] , dp[i-1]) | ||
|
|
||
| return dp[len(nums) - 1] | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| # dp[i] = max(dp[i-1], dp[i-2] + num[i]) | ||
| # TC : O(n) | ||
| # SC : O(n) | ||
|
|
||
| class Solution: | ||
| def rob(self, nums: List[int]) -> int: | ||
| dp = [0] * len(nums) | ||
|
|
||
| dp[0] = nums[0] | ||
|
|
||
| if len(nums) == 1: | ||
| return dp[0] | ||
|
|
||
| dp[1] = max(nums[0],nums[1]) | ||
|
|
||
| for i in range(2, len(nums)): | ||
| dp[i] = max(dp[i-1], dp[i-2] + nums[i]) | ||
|
|
||
| return max(dp) | ||
|
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. 솔루션이 2개 있는데 마지막 라인만 다르네요. 혹시 max로 값을 추출하신 이유가 있을까요? |
||
|
|
||
|
|
||
|
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(n) | O(n) | ✅ |
피드백: 중복 제거를 통해 각 원소에 대해 한 번씩만 확장 탐색한다. 해시셋으로 조회가 빠르다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 2: Solution.longestConsecutive — Time: O(n) / Space: O(n)
| 복잡도 | |
|---|---|
| Time | O(n) |
| Space | O(n) |
피드백: 시작점만 확장하는 방식으로 중복 탐색을 피한다. 시간 복잡도는 최상으로 유지된다.
개선 제안: 현재 구현이 적절해 보입니다.
💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!
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.
깔끔하네요 배워갑니다👍
|
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(nlogn) | O(n log n) | ✅ |
| Space | O(n) | O(n) | ✅ |
피드백: 해시 맵으로 빈도수를 수집하고, 정렬으로 상위 k개를 얻는다. 구현이 명확하지만 정렬은 비용이 더해진다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 2: Solution.topKFrequent — Time: O(n log n) / Space: O(n)
| 복잡도 | |
|---|---|
| Time | O(n log n) |
| Space | O(n) |
피드백: 정렬 기반 방법으로 간결하지만 최악의 경우 비용이 큼.
개선 제안: 현재 구현이 적절해 보입니다.
💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!
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.
문제에 시간 복잡도 n log n보다 낮게 풀어보라는 코멘트가 있던 것 같아요! 시간 복잡도 최적화 고민해보시면 좋을 것 같습니다.
이미 아실 수도 있지만, 파이썬 collections 모듈에서 Counter를 제공하고 있어서 숫자 갯수 셀때 사용하시면 편리합니다.
https://wikidocs.net/233689
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.rob— Time: ✅ O(n) → O(n) / Space: ✅ O(n) → O(n)피드백: 두 가지 경우를 비교하며 현재 위치를 포함하는지 여부에 따라 최댓값을 갱신한다. 배열 크기에 따라 선형 시간 복잡도와 선형 공간 복잡도가 나타난다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 2:
Solution.rob— Time: O(n) / Space: O(n)피드백: 초기화와 루프 흐름이 명확하며 같은 DP 아이디어를 사용하지만 배열 크기와 반환 부분에서 차이가 있다.
개선 제안: 현재 구현이 적절해 보입니다.
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.
@sangbeenmoon 공간 복잡도를 O(1)로 최적화할 수 있는 풀이도 있는 것 같습니다!