Skip to content

Conversation

Satorien
Copy link
Owner

@Satorien Satorien commented Aug 5, 2025

for i in range(1, len(prices)):
price_diff = prices[i] - prices[i-1]
if price_diff > 0:
profit += price_diff

Choose a reason for hiding this comment

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

いいと思いました。
完全に趣味ですが、price_diffよりはprofitの方が好みでした。差に関する変数だという意味にプラスして、当日-前日なのか、前日-当日なのかが伝わる気がするので。

Copy link
Owner Author

Choose a reason for hiding this comment

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

確かにそういう考え方もありますね、ありがとうございます!


- ある点までの利益とその後の利益を足す?
- 何回も足す必要があるしO(n)を何回かやることになる
- よく考えると上がるところを全部計上すれば良いだけ
Copy link

@Mike0121 Mike0121 Aug 24, 2025

Choose a reason for hiding this comment

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

これに気づけると楽ですよね。自分は、この手の少し算数的(貪欲法)な問題は各パターンを手書きして考えてみています。

class Solution:
def maxProfit(self, prices: List[int]) -> int:
profit = 0
buy = prices[0]

Choose a reason for hiding this comment

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

変数名、buyは、動詞単体のため少しわかりづらいと感じました。buy_priceなどはどうでしょうか?

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.

3 participants