Skip to content

Create best-time-to-buy-and-sell-stock-ii.md#22

Open
tshimosake wants to merge 1 commit intomasterfrom
tshimosake-patch-20
Open

Create best-time-to-buy-and-sell-stock-ii.md#22
tshimosake wants to merge 1 commit intomasterfrom
tshimosake-patch-20

Conversation

@tshimosake
Copy link
Owner

return total_profit
```

いや、貪欲でできる理由がよくわかっていなかった。最適解を $\sum_{k=1}^{n-1} \max(0, p_k - p_{k-1})$ と分解できることが本質だった。

Choose a reason for hiding this comment

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

なので、ifによる条件分岐を外し、単にtotal_profit += max(0, prices[today] - prices[today - 1])としてもいいですね。

Comment on lines +29 to +39
next_profit_without_stock = max(
profit_without_stock, # そのまま買わない
profit_with_stock + prices[today] # 売る
)
next_profit_with_stock = max(
profit_with_stock, # 売らずに持ち続ける
profit_without_stock - prices[today] # 新たに買う
)
profit_without_stock, profit_with_stock = (
next_profit_without_stock, next_profit_with_stock
)

Choose a reason for hiding this comment

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

この辺の更新ロジックを関数化するとより読みやすくなるかもしれません。

```

## step3
最初の step1 がもっとも素直だと思ったので、これを採用する

Choose a reason for hiding this comment

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

売買に回数制限がある場合などはDPの方法が有用になったりします。

Arai 60には含まれていないですが、よければ以下もどうぞ。

https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます、いったん完走したらやってみたいです!

いや、貪欲でできる理由がよくわかっていなかった。最適解を $\sum_{k=1}^{n-1} \max(0, p_k - p_{k-1})$ と分解できることが本質だった。

## step2
ほかの回答は長くてなかなか読む気にならない…

Choose a reason for hiding this comment

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

こういう意見もあります。個人的にも同意できる考え方です。

ついでなのでここに書いておくが、一般的にコードは書くよりも読むほうがずっと大切で重要である。読んで理解するためには書くよりもどうしてもある面では高い能力が必要になる。自分で書くときにはどう書くかは制限できるが、他人がどう書くかは読んで理解するまでは分からないからである。

https://nuc.hatenadiary.org/entry/2025/11/29#二分探索を読めるか

読む方が大事

https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.14wn5hdkkebn

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます、少しずつ読めるようになりたいです

Choose a reason for hiding this comment

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

関数型っぽく書いてみました。

import itertools


class Solution1:
    def maxProfit(self, prices: list[int]) -> int:
        return sum(
            price_today - price_yesterday
            for price_yesterday, price_today
            in itertools.pairwise(prices)
            if price_today > price_yesterday
        )


class Solution2:
    def maxProfit(self, prices: List[int]) -> int:
        return sum(map(lambda p: max(0, p[1] - p[0]), itertools.pairwise(prices)))

Copy link

Choose a reason for hiding this comment

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

電車の移動時間などで人の PR 読んでいたらいいのだと思います。

if prices[today] > prices[today - 1]:
total_profit += prices[today] - prices[today - 1]
return total_profit
```

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.

4 participants