-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: main
Are you sure you want to change the base?
Conversation
if not prices: | ||
return 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.
priceが空でもfor文に入らず初期値の0が返るので不要かと思いました。
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.
良いと思います
return 0 | ||
max_profit = 0 | ||
for i in range(1, len(prices)): | ||
difference = prices[i] - prices[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.
確かにdifferenceではあるんですけど、profit, gainなどの方がよりこの値の指している意味が伝わるかなと思います。
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.
差額が0より大きければprofitに加算する、と考えて書いてみたんですが、微妙ですかね?
pay the difference 的なイメージです。
(prices[i] - prices[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.
自分英語の細かいニュアンスまでは分からなくて、とはいえdifferenceでも良いとは思います。
differenceだと値の向きが変数だけだと分からないかなというのが個人的な感覚でした。differenceがプラスの場合にprofit or lossのどちらを意味するのかというのが分からず、それだったらprofitと命名してあげる方が理解がすぐできるかなと思いました。個人的にはprofitがマイナスになっても特段違和感がないです。
- 空間計算量: O(1) | ||
|
||
```py | ||
class Solution: |
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.
自分は 2nd の解法で解いたのですが、 1st の解法のほうがシンプルで良いと思いました。
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/