Skip to content

122. Best Time to Buy and Sell Stock II #39

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

fhiyo
Copy link
Owner

@fhiyo fhiyo commented Jul 8, 2024

Comment on lines +66 to +67
if not prices:
return 0

Choose a reason for hiding this comment

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

priceが空でもfor文に入らず初期値の0が返るので不要かと思いました。
priceが空のときの例外処理を想定しての記載でしたら問題ないです。

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.

良いと思います

return 0
max_profit = 0
for i in range(1, len(prices)):
difference = prices[i] - prices[i - 1]

Choose a reason for hiding this comment

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

確かにdifferenceではあるんですけど、profit, gainなどの方がよりこの値の指している意味が伝わるかなと思います。

Copy link
Owner Author

Choose a reason for hiding this comment

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

差額が0より大きければprofitに加算する、と考えて書いてみたんですが、微妙ですかね?
pay the difference 的なイメージです。
(prices[i] - prices[i - 1]が負になることもあるので差額の方が自分的にしっくり来た)

Choose a reason for hiding this comment

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

自分英語の細かいニュアンスまでは分からなくて、とはいえdifferenceでも良いとは思います。

differenceだと値の向きが変数だけだと分からないかなというのが個人的な感覚でした。differenceがプラスの場合にprofit or lossのどちらを意味するのかというのが分からず、それだったらprofitと命名してあげる方が理解がすぐできるかなと思いました。個人的にはprofitがマイナスになっても特段違和感がないです。

- 空間計算量: O(1)

```py
class Solution:
Copy link

Choose a reason for hiding this comment

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

自分は 2nd の解法で解いたのですが、 1st の解法のほうがシンプルで良いと思いました。

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