-
Notifications
You must be signed in to change notification settings - Fork 0
Solved: 122. Best Time to Buy and Sell Stock II #38
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) == 1: | ||
| return 0 | ||
| profit = 0 | ||
| yesterday_price = 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.
どっちでも良いですが、prev_priceのほうが好みです。
| if len(prices) == 1: | ||
| return 0 | ||
| profit = 0 | ||
| prev_price = 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.
prev_price を定義せず
for day in range(len(prices) - 1):
profit += max(0, prices[day + 1] - prices[day])で回してもよいと思いました。
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.
レビューありがとうございます。
dayでループすると、前日と当日を比べていることが直感的に理解できてとても良いですね。
| if len(prices) == 1: | ||
| 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.
あってもいいですが、これはなくてもいいですね。
| for i in range(1, len(prices)): | ||
| if prices[i] >= prices[i - 1]: | ||
| continue | ||
| profit += max(0, prices[i - 1] - buy_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.
このロジックだと、prices[i - 1] - buy_priceが負になることはなさそうなので、maxとの比較はいらないかもと思いました
122. Best Time to Buy and Sell Stock II