Skip to content

Conversation

@TORUS0818
Copy link
Owner

max_profit = 0
for price in prices:
if prev_price < price:
max_profit += (price - prev_price)
Copy link

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

Copy link
Owner Author

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]
Copy link

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 で処理がスキップされるところまで理解できれば、正しく動くことが分かるのですが、ややパズルに感じました。

やや仰々しいですが、最初の日かどうかを表すフラグを立てるのはいかがでしょうか?

Copy link
Owner Author

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)
Copy link

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
```
思考ログ:
- 以前はこういう処理を関数に分けずに書き連ねてバグらせる、というのがよくあった
Copy link

Choose a reason for hiding this comment

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

複雑さへの対処ができていないイメージですね。
頭で保持できる量よりも小さいものであったとしても、たくさん持っているといずれ溢れるので、適切に荷物を下ろしていかないといけないんですが、持ったまま別の簡単なことを始めて溢れるイメージを持っています。

Copy link
Owner Author

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の略語

Choose a reason for hiding this comment

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

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.

pricesが(狭義)単調減少の場合、どう足掻いてもlossになるので一応このようにしましたが、考え過ぎかもしれません。

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なのか分からなくて混乱してしまいます

Copy link
Owner Author

Choose a reason for hiding this comment

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

収益計算の結果にprofit and loss(pl)という表現をよく使うので私にとってはこちらの方が馴染みはあります。
が、一般的な感覚でないのかもしれません。

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/

Choose a reason for hiding this comment

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

例えば、enabled_or_disabledという変数がtrueだったら、どっちなんだと混乱しませんか?

Copy link
Owner Author

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:

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 i

Copy link
Owner Author

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
Copy link

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とします。

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.

7 participants