Skip to content

121.Best-Time-to-Buy-and-Sell-Stock#35

Open
tom4649 wants to merge 2 commits intomainfrom
121.Best-Time-to-Buy-and-Sell-Stock
Open

121.Best-Time-to-Buy-and-Sell-Stock#35
tom4649 wants to merge 2 commits intomainfrom
121.Best-Time-to-Buy-and-Sell-Stock

Conversation

@tom4649
Copy link
Copy Markdown
Owner

@tom4649 tom4649 commented Mar 29, 2026

max_price_in_current_or_future = -float("inf")
max_profits = []
for price in prices[::-1]:
max_price_in_current_or_future = max(max_price_in_current_or_future, price)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

in_current_or_future の意味を考えてしまいました。この短さなら max_price でもいいかもしれません。逆順に見ている文脈もすぐ上にありますし。

max_profits = []
for price in prices[::-1]:
max_price_in_current_or_future = max(max_price_in_current_or_future, price)
max_profits.append(max_price_in_current_or_future - price)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

これは、max_profit: int を更新していくのではダメでしたっけ。過去のmaxをlistで保持しておく必要が無い気がしました。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

その通りですね。メモリ使用量を削減できます。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants