Skip to content

Conversation

@TORUS0818
Copy link
Owner

Copy link

@hayashi-ay hayashi-ay left a comment

Choose a reason for hiding this comment

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

良いと思います

@TORUS0818
Copy link
Owner Author

@hayashi-ay
いつも有難うございます!

Comment on lines +149 to +150
min_price = min(min_price, price)
max_profit = max(max_profit, price - min_price)

Choose a reason for hiding this comment

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

You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.

同日に買って売れないので、この2行入れ替えた方が正確だと思います。

Copy link
Owner Author

Choose a reason for hiding this comment

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

これは気づいていませんでした。
この更新したmin_priceは本来使えない価格なわけですね。

尚、更新された場合はmin_price = priceとなるので、max_profit = max(max_profit, 0)となり問題が起きなかったわけですね。

def maxProfit(self, prices: List[int]) -> int:
min_price = prices[0]
max_profit = 0
for price in prices:

Choose a reason for hiding this comment

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

すでにインデックス0の値は見ているので、インデックス1から始めた方が正確だと思います。

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:]とスライスを作るよりはいいかなということでこういう実装になったのですが、やってることが素直でなくなってしまいました。。

Copy link

Choose a reason for hiding this comment

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

math.inf を入れておくのは一つです。型が違うものが入ることになるので好まない人もいるでしょうが。

Copy link
Owner Author

Choose a reason for hiding this comment

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

https://docs.python.org/ja/3/library/sys.html#sys.maxsize
ここら辺も選択肢にありましたが、これはこれで素直ではない気がしまして。

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.

5 participants