Skip to content
Merged
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
21 changes: 21 additions & 0 deletions best-time-to-buy-and-sell-stock/freemjstudio.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
  • 설명: 최초 풀이엔 시간 복잡도 문제로 비효율적이지만, 두 번째 풀이에서 매일 최소 가격을 갱신하며 현재 가격과의 차이를 비교하는 방식은 그리디 패턴의 전형으로, 한 번의 순회로 최적해를 구한다. 또한 1차원 배열을 좌우로 단순한 포인터처럼 다루는 접근으로 간주 가능하다.

📊 시간/공간 복잡도 분석

복잡도
Time O(n)
Space O(1)

피드백: 첫 시도는 모든 가능한 구매 시점을 기반으로 최대 이익을 계산해 시간 복잡도가 O(n^2)로 비효율적이다. 두 번째 시도에서 최소 가격과 누적 이익을 추적해 선형 시간으로 해결한다.

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

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

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

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.

i가 0인 경우 불필요한 재계산이 되서 1부터 시작하면 명확할 거 같습니다.

profit = prices[i] - min_price
max_profit = max(max_profit, profit)
return max_profit
Comment on lines +1 to +21

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.

타임아웃 과정부터 성공한 솔루션까지 같이 보여주셔서 비교하기 좋았습니다.
시간/공간 복잡도 분석도 주석으로 같이 해주시면 더 좋을 것 같아요!
수고하셨습니다.

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.

앞으로는 시간/공간복잡도 분석도 추가해보겠습니다
감사합니다 !!

Loading