Skip to content
Open
Changes from all commits
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.

🏷️ 알고리즘 패턴 분석

  • 패턴: Greedy, Two Pointers
  • 설명: 최소 가격을 추적하며 현재 가격과의 차이로 이익을 갱신하는 방식으로, 한 번의 순회로 최적 해를 구하는 그리디 패턴에 해당합니다. 또한 가격을 한 방향으로 스캔하며 최대 이익을 계산하므로 투 포인터의 간단한 변형으로 볼 수 있습니다.

📊 시간/공간 복잡도 분석

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

피드백: 초기 최소 가격을 설정하고 가격이 오를 때마다 이익을 갱신하는 단일 순회 방식이다.

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

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.

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

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)

return max_profit
Loading