Skip to content
Merged
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
16 changes: 16 additions & 0 deletions best-time-to-buy-and-sell-stock/devyejin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# time complexity : O(n)
# space complexity : O(1)
from typing import List


class Solution:
def maxProfit(self, prices: List[int]) -> int:
n = len(prices)
min_price = prices[0]
Copy link
Contributor

Choose a reason for hiding this comment

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

prices가 빈 배열일 경우 prices[0] 접근에서 IndexError 발생 가능 → 입력 검증 필요

Copy link
Contributor Author

Choose a reason for hiding this comment

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

문제 조건에서 prices 길이가 1이상이라는 문구가 있어서 저런식으로 처리하긴했는데, 그래도 안전하게 검증 로직 넣는게 좋을까요?

Copy link
Contributor

Choose a reason for hiding this comment

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

아 제가 조건을 못 봤네요. 이 문제에서는 해당 사항 고려하지 않으셔도 됩니다.

max_profit = 0

for i in range(1, n):
min_price = min(min_price, prices[i])
max_profit = max(max_profit, prices[i] - min_price)

return max_profit