-
Notifications
You must be signed in to change notification settings - Fork 0
122. Best Time to Buy and Sell Stock II #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
| max_profit = 0 | ||
| for price in prices: | ||
| if prev_price < price: | ||
| max_profit += (price - prev_price) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
右辺の () は外してよいと思います。
max_profit += price - prev_price
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
無意識で付けてしまいました。。
| ```python | ||
| class Solution: | ||
| def maxProfit(self, prices: List[int]) -> int: | ||
| prev_price = prices[0] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
prev_price を最初の price で初期化している点に違和感を感じました。 prev_price < price で処理がスキップされるところまで理解できれば、正しく動くことが分かるのですが、ややパズルに感じました。
やや仰々しいですが、最初の日かどうかを表すフラグを立てるのはいかがでしょうか?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
有難うございます。
完全にパズルですね。。
最初はprices[1]から処理するようなコードを書いていたのですが、まとめてできないか検討したところ、prev_price = prices[0]とおけば問題なく動くことが分かり、記述もシンプルになることからこのように変更しました。
が、その結果、自分が解いたパズルをコードに埋め込むことになってしましました。。
| i = 0 | ||
| max_profit = 0 | ||
| while i < len(prices): | ||
| valley_i = find_next_valley(i) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i は略さず index としたほうが、読むにあたり認知負荷が低くなると思います。
| return max_profit | ||
| ``` | ||
| 思考ログ: | ||
| - 以前はこういう処理を関数に分けずに書き連ねてバグらせる、というのがよくあった |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
複雑さへの対処ができていないイメージですね。
頭で保持できる量よりも小さいものであったとしても、たくさん持っているといずれ溢れるので、適切に荷物を下ろしていかないといけないんですが、持ったまま別の簡単なことを始めて溢れるイメージを持っています。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
言語化有難うございます。
原因は脳のワーキングメモリの使い方にあったんですね。
まだ完全ではないですが、上記のようなことを意識してから明らかに解ける問題が増えました。
| ```python | ||
| class Solution: | ||
| def maxProfit(self, prices: List[int]) -> int: | ||
| # pl: profit & lossの略語 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
profitだけで良いと思います
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pricesが(狭義)単調減少の場合、どう足掻いてもlossになるので一応このようにしましたが、考え過ぎかもしれません。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
profitという変数でマイナスの値を取ったら、損失だなーというのは分かると思いますが、そういう話ではないですか?自分はprofit_lossという変数がたとえば5だとしてこれがprofitなのかlossなのか分からなくて混乱してしまいます
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
収益計算の結果にprofit and loss(pl)という表現をよく使うので私にとってはこちらの方が馴染みはあります。
が、一般的な感覚でないのかもしれません。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
林さんの意見に+1。利益がマイナスだと、赤字の意味だと思います。
https://www.yayoi-kk.co.jp/kaikei/oyakudachi/accounting-settlement-03/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
例えば、enabled_or_disabledという変数がtrueだったら、どっちなんだと混乱しませんか?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
コメント有難うございます。
成程、少し私の感覚がズレてそうです。
| ```python | ||
| class Solution: | ||
| def maxProfit(self, prices: List[int]) -> int: | ||
| def find_next_valley(start: int) -> int: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
自分ならwhileの中のifもwhileに含めるかなと思います。
def find_next_valley(start: int) -> int:
i = start
while i < len(prices) - 1 and prices[i] >= prices[i + 1]:
i += 1
return iThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
有難うございます。
確かにこの場合は2つに分けなくてもいいですね。
| peak_i = find_next_peak(valley_i) | ||
| sell_price = prices[peak_i] | ||
| max_profit += sell_price - buy_price | ||
| i = peak_i + 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
peak_iの探索はvalley_iのちょうどのindexから始まってるので(L119)、自分ならこっちもi = peak_iにして統一感を出し、while i < len(prices) - 1とします。
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/