Create best-time-to-buy-and-sell-stock-ii.md#22
Open
tshimosake wants to merge 1 commit intomasterfrom
Open
Conversation
naoto-iwase
reviewed
Jan 4, 2026
| return total_profit | ||
| ``` | ||
|
|
||
| いや、貪欲でできる理由がよくわかっていなかった。最適解を $\sum_{k=1}^{n-1} \max(0, p_k - p_{k-1})$ と分解できることが本質だった。 |
There was a problem hiding this comment.
なので、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 | ||
| ) |
| ``` | ||
|
|
||
| ## step3 | ||
| 最初の step1 がもっとも素直だと思ったので、これを採用する |
There was a problem hiding this comment.
売買に回数制限がある場合などはDPの方法が有用になったりします。
Arai 60には含まれていないですが、よければ以下もどうぞ。
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/
Owner
Author
There was a problem hiding this comment.
ありがとうございます、いったん完走したらやってみたいです!
| いや、貪欲でできる理由がよくわかっていなかった。最適解を $\sum_{k=1}^{n-1} \max(0, p_k - p_{k-1})$ と分解できることが本質だった。 | ||
|
|
||
| ## step2 | ||
| ほかの回答は長くてなかなか読む気にならない… |
There was a problem hiding this comment.
こういう意見もあります。個人的にも同意できる考え方です。
ついでなのでここに書いておくが、一般的にコードは書くよりも読むほうがずっと大切で重要である。読んで理解するためには書くよりもどうしてもある面では高い能力が必要になる。自分で書くときにはどう書くかは制限できるが、他人がどう書くかは読んで理解するまでは分からないからである。
https://nuc.hatenadiary.org/entry/2025/11/29#二分探索を読めるか
読む方が大事
Owner
Author
There was a problem hiding this comment.
ありがとうございます、少しずつ読めるようになりたいです
There was a problem hiding this comment.
関数型っぽく書いてみました。
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)))
tokuhirat
reviewed
Jan 5, 2026
| if prices[today] > prices[today - 1]: | ||
| total_profit += prices[today] - prices[today - 1] | ||
| return total_profit | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/