-
Notifications
You must be signed in to change notification settings - Fork 0
Solved: 121. Best Time to Buy and Sell Stock #37
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 len(prices) < 2: | ||
| return 0 | ||
| max_profit = 0 | ||
| lowest_profit_so_far = 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.
max_profitにつられて、lowest_price_so_farと間違えてますね。
| return 0 | ||
| max_profit = 0 | ||
| min_price_so_far = prices[0] | ||
| for price in prices[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.
これスライスしなくても動きそうですね。
番兵でも書けそうです。
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 = 0として、len(nums)==1の時はそのまま0で返すのが良さそうです。
| if len(prices) < 2: | ||
| 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.
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.
レビューありがとうございます。
1の場合のみであるという意図は伝わりにくいですね。
おっしゃる通り、if len(prices) == 1: の方が素直ですね。
あと、なくても動きますね。
2日以上のデータが与えられたときのことを考えて67,68を書きましたが、
step2,3をやる中で、なくてもよいことに気付くべきでした。
121. Best Time to Buy and Sell Stock