Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions best-time-to-buy-and-sell-stock/dolphinflow86.py

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

깔끔하게 잘 해결해 주셨네요!
best time to buy and sell stock, 즉 해당 문제는
뒤에 로마 숫자를 붙혀서 1, 2, 3, 4, 5 총 다섯종류가 있는데요
dp 연습하기에 정말 괜찮은 문제라고 생각해서
2번문제
II는 한번 풀어보시길 추천드려요!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오! 네 문제 추천 감사합니다! 한번 풀어볼게요 👍

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

  • 패턴: Two Pointers, Greedy, Hash Map / Hash Set
  • 설명: 주어진 코드는 단순히 현재 최소가를 유지하며 가격을 순회하면서 최대 이익을 갱신하는 방식으로 구현되어 있습니다. 한 번의 순회로 최적해를 구하는 그리디 접근이며, 투 포인터의 개념으로 좌우의 값을 관리합니다.

📊 시간/공간 복잡도 분석

유저 분석 실제 분석 결과
Time O(N) O(n)
Space O(1) O(1)

피드백: 한 번의 순회를 통해 현재 가격과 최소 가격을 비교하며 최대 이익을 갱신합니다.

개선 제안: 현재 구현이 적절해 보입니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# 1) Keep track of local minimum and use local minimum to update max profile while interating prices.
# TC: O(N) where N is the length of prices
# SC: O(1)
class Solution:
def maxProfit(self, prices: List[int]) -> int:
min_price = prices[0]
max_profit = 0

for price in prices:
max_profit = max(max_profit, price - min_price)
min_price = min(min_price, price)
Comment on lines +9 to +11

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

사소하지만 더 최적화 할 수 있는 부분은 min, max 일 거 같아요. 고민해보셔도 좋을 거 같습니다!


return max_profit
Loading