-
-
Notifications
You must be signed in to change notification settings - Fork 362
[freemjstudio] WEEK 05 Solutions #2773
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 |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # first attempt - time out | ||
|
|
||
| class Solution: | ||
| def maxProfit(self, prices: List[int]) -> int: | ||
| max_profit = 0 | ||
| for i in range(len(prices)): | ||
| profit = max(prices[i:]) - prices[i] | ||
| max_profit = max(max_profit, profit) | ||
| return max_profit | ||
|
|
||
| # second attempt - greedy approach | ||
| class Solution: | ||
| def maxProfit(self, prices: List[int]) -> int: | ||
| max_profit = 0 | ||
| min_price = prices[0] | ||
|
|
||
| for i in range(len(prices)): | ||
| min_price = min(min_price, prices[i]) | ||
|
Comment on lines
+14
to
+18
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. i가 0인 경우 불필요한 재계산이 되서 1부터 시작하면 명확할 거 같습니다. |
||
| profit = prices[i] - min_price | ||
| max_profit = max(max_profit, profit) | ||
| return max_profit | ||
|
Comment on lines
+1
to
+21
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. 앞으로는 시간/공간복잡도 분석도 추가해보겠습니다 |
||
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)로 비효율적이다. 두 번째 시도에서 최소 가격과 누적 이익을 추적해 선형 시간으로 해결한다.
개선 제안: 현재 구현이 적절해 보입니다.