-
Notifications
You must be signed in to change notification settings - Fork 0
Create 0122-best-time-to-buy-and-sell-stock-ii.md #43
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
Open
docto-rin
wants to merge
1
commit into
main
Choose a base branch
from
0122-best-time-to-buy-and-sell-stock-ii
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
0122-best-time-to-buy-and-sell-stock-ii/0122-best-time-to-buy-and-sell-stock-ii.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| ## Step 1 | ||
|
|
||
| - 問題文 | ||
| - 前回の問題に、売買の回数上限がなくなった版。(ただし、常に最大1株しか持てない。) | ||
| - 前回の問題文: | ||
| - 非負整数の配列`prices`が与えられる。prices[i]はi日目の株価である。 | ||
| - どこかの日で株を買い、それより未来の日で株を売って得られる収益の最大値を知りたい。 | ||
| - 取引は1回までできる。(`prices`が単調増加する場合などで)取引しない場合は収益は0になる。 | ||
| - 制約: | ||
| - 1 <= prices.length <= 3 * 10^4 | ||
| - 0 <= prices[i] <= 10^4 | ||
|
|
||
| ### 実装1 | ||
|
|
||
| - アルゴリズムの選択 | ||
| - 一見複雑だが、これも一手先の未来をみるようにすれば貪欲的に解けそう。 | ||
| - 株の教科書には大体「株価の谷で買い、山で売るのが理想。だけど実際には株価が次上がるか下がるかわからないのでそれは無理で、...」みたいな書き出しがある。 | ||
| - しかし今回は変動が完全に見える。(プロットすれば山谷がわかる状態。) | ||
| - 具体的には、prices[i + 1] - prices[i]の正負がこれまでと切り替わるタイミングで売買をすればいい。 | ||
| - 実装 | ||
| - forループで1周すればいける。 | ||
| - 最後の山or谷の価格(極値)と増加中or減少中のフラグを管理すれば貪欲的に売買可能。 | ||
| - 価格差が0のときは、売買してもしなくても正しいが、わかりやすくしないことにする。 | ||
| - 計算量 | ||
| - Time: O(n) | ||
| - Space: O(1) | ||
|
|
||
| ```python3 | ||
| from typing import List | ||
|
|
||
|
|
||
| class Solution: | ||
| def maxProfit(self, prices: List[int]) -> int: | ||
| if not prices: | ||
| return 0 | ||
|
|
||
| total_profit = 0 | ||
| extremum = prices[0] | ||
| increasing = True | ||
| for i in range(len(prices) - 1): | ||
| if increasing: | ||
| if prices[i + 1] - prices[i] >= 0: | ||
| continue | ||
| total_profit += prices[i] - extremum | ||
| extremum = prices[i] | ||
| increasing = False | ||
| continue | ||
|
|
||
| if prices[i + 1] - prices[i] <= 0: | ||
| continue | ||
| extremum = prices[i] | ||
| increasing = True | ||
|
|
||
| if increasing: | ||
| total_profit += prices[-1] - extremum | ||
| return total_profit | ||
| ``` | ||
|
|
||
| - ここまで25分ほど。(動くコードは20分くらいで書けたが、リファクタした) | ||
|
|
||
| ## Step 2 | ||
|
|
||
| - レビュー by GPT-5 | ||
| - > 「上がった日だけ都度差分を足す」貪欲解が最適。 | ||
| - 確かに。現実の株の売買回数を減らしたい気持ちが邪魔してこれは思いつかなかった。 | ||
|
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. 株を持って日を超すか持たないで日を超すかの2択なので、それぞれどちらがよかったかを翌日に決めればいいということです。 |
||
|
|
||
| ## Step 3 | ||
|
|
||
| ```python3 | ||
| from typing import List | ||
|
|
||
|
|
||
| class Solution: | ||
| def maxProfit(self, prices: List[int]) -> int: | ||
| if not prices: | ||
| return 0 | ||
|
|
||
| total_profit = 0 | ||
| for i in range(1, len(prices)): | ||
| total_profit += max(0, prices[i] - prices[i - 1]) | ||
| return total_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. 読みやすかったです。 |
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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] の情報は使われていないと思うのでこの行は消してもいいのかなと思いました。
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.
ありがとうございます。
正しいです。削除すべきですね。