-
Notifications
You must be signed in to change notification settings - Fork 0
Solved Arai60/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
…t Time to Buy and Sell Stock II.md
for i in range(1, len(prices)): | ||
price_diff = prices[i] - prices[i-1] | ||
if price_diff > 0: | ||
profit += price_diff |
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_diffよりはprofitの方が好みでした。差に関する変数だという意味にプラスして、当日-前日なのか、前日-当日なのかが伝わる気がするので。
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.
確かにそういう考え方もありますね、ありがとうございます!
|
||
- ある点までの利益とその後の利益を足す? | ||
- 何回も足す必要があるしO(n)を何回かやることになる | ||
- よく考えると上がるところを全部計上すれば良いだけ |
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.
これに気づけると楽ですよね。自分は、この手の少し算数的(貪欲法)な問題は各パターンを手書きして考えてみています。
class Solution: | ||
def maxProfit(self, prices: List[int]) -> int: | ||
profit = 0 | ||
buy = 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.
変数名、buyは、動詞単体のため少しわかりづらいと感じました。buy_price
などはどうでしょうか?
問題文:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/