-
Notifications
You must be signed in to change notification settings - Fork 0
Create 122. Best Time to Buy and Sell Stock II.md #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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| # 122. Best Time to Buy and Sell Stock II | ||
| ## STEP1 | ||
| - 何も見ずに解いてみる | ||
| - 前日より増加している分を足し合わせればよい。 | ||
| ```python | ||
| class Solution: | ||
| def maxProfit(self, prices: List[int]) -> int: | ||
| max_profit = 0 | ||
| for i in range(len(prices) - 1): | ||
| if prices[i] < prices[i + 1]: | ||
| max_profit += prices[i + 1] - prices[i] | ||
| return max_profit | ||
| ``` | ||
| #### memo | ||
| 時間計算量: $O(n)$ | ||
| 空間計算量: $O(1)$ | ||
|
|
||
| ## STEP2 | ||
| ### プルリクやドキュメントを参照 | ||
| - https://github.com/hayashi-ay/leetcode/pull/56/files | ||
| - https://github.com/olsen-blue/Arai60/pull/38/files | ||
| - 株を持っている/いない場合の保有現金最大値を管理する方法 | ||
| - https://github.com/olsen-blue/Arai60/pull/38/files#r1980562479 | ||
| - これを読むまでは全然わからなかった。変数名大事ですね。 | ||
| - 遷移としては、株を持っている/持っていない、から売る/買う、そのままの4通りある。STEP1の書き方だと単純に利益が出る時に売買するのでシンプルな気がする。 | ||
| - ピークと谷を見つける書き方もある。毎日売り買いするよりも自然かもしれない。 | ||
| ```python | ||
| class Solution: | ||
| def maxProfit(self, prices: List[int]) -> int: | ||
| def find_next_highest(index: int) -> int: | ||
| while index < len(prices) - 1 and prices[index] <= prices[index + 1]: | ||
| index += 1 | ||
| return index | ||
|
|
||
| def find_next_lowest(index: int) -> int: | ||
| while index < len(prices) - 1 and prices[index + 1] <= prices[index]: | ||
| index += 1 | ||
| return index | ||
|
|
||
| max_profit = 0 | ||
| index = 0 | ||
| while index < len(prices): | ||
| valley_index = find_next_lowest(index) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. peakの対義語として、bottomでも良いかもしれません。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ありがとうございます。確かに関数名と合わせた方がわかりやすいですね。 |
||
| peak_index = find_next_highest(valley_index) | ||
| max_profit += prices[peak_index] - prices[valley_index] | ||
| index = peak_index + 1 | ||
| return max_profit | ||
| ``` | ||
|
|
||
| ## STEP3 | ||
| ### 3回ミスなく書く | ||
| ```python | ||
| class Solution: | ||
| def maxProfit(self, prices: List[int]) -> int: | ||
| max_profit = 0 | ||
| for i in range(len(prices) - 1): | ||
| if prices[i] < prices[i + 1]: | ||
| max_profit += prices[i + 1] - prices[i] | ||
| return max_profit | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 特に問題ないと思います。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. day は自分の選択肢にはなかったので今後は選択肢に入れるようにします。ありがとうございます。 |
||
| ``` | ||
|
|
||
| 2分で1回Accept (STEP1と同じため省略) | ||
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.
DPでも(個人的に少し直感的ではないですが)解けるので、解いてみても良いかと思いました。
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.
DP で書いてみました。私もあまり直観的ではないなと感じています。