-
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?
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,95 @@ | ||
## Step 1. Initial Solution | ||
|
||
- ある点までの利益とその後の利益を足す? | ||
- 何回も足す必要があるしO(n)を何回かやることになる | ||
- よく考えると上がるところを全部計上すれば良いだけ | ||
- これで実装して以下の通り | ||
- prices[0]で初期化するべきかは悩んだがその日のうちに売ることもできるという文脈を踏まえて含めた | ||
- ただこのままだと前日に買っていてかつそれが今日より安かったら売る、という状況になっていて現実の動作には即していない | ||
|
||
```python | ||
class Solution: | ||
def maxProfit(self, prices: List[int]) -> int: | ||
prev_price = prices[0] | ||
profit = 0 | ||
for price in prices: | ||
if price > prev_price: | ||
profit += price - prev_price | ||
prev_price = price | ||
return profit | ||
``` | ||
|
||
### Complexity Analysis | ||
|
||
- 時間計算量:O(n) | ||
- 空間計算量:O(1) | ||
|
||
## Step 2. Alternatives | ||
|
||
- もっとシンプルに書くならこれでも良い | ||
|
||
```python | ||
class Solution: | ||
def maxProfit(self, prices: List[int]) -> int: | ||
profit = 0 | ||
for i in range(1, len(prices)): | ||
if prices[i] > prices[i-1]: | ||
profit += prices[i] - prices[i-1] | ||
return profit | ||
``` | ||
|
||
- とても当たり前だがなぜか上の書き方で書いてしまった | ||
- iで動かそうとしたら書けていたかも | ||
- +αできかれそうなこと | ||
|
||
> この利益を実現するための最小売買回数を求められますか? | ||
> | ||
- https://github.com/Yoshiki-Iwasa/Arai60/pull/53/files#r1730194725 | ||
- 上がり始めたから下がるまでが1回でこれをカウントしていく | ||
|
||
```python | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more. 変数名、buyは、動詞単体のため少しわかりづらいと感じました。 |
||
hold = False | ||
transactions = 0 | ||
for i in range(1, len(prices)): | ||
if prices[i] < prices[i-1]: | ||
if hold: | ||
profit += prices[i - 1] - buy | ||
hold = False | ||
transactions += 1 | ||
buy = prices[i] | ||
else: | ||
hold = True | ||
if hold: | ||
profit += prices[-1] - buy | ||
transactions += 1 | ||
return profit, transactions | ||
``` | ||
|
||
- 変数名 | ||
- 山と谷というイメージ | ||
- topとbottomという表現はちょうど良い | ||
- https://github.com/olsen-blue/Arai60/pull/38/files | ||
- 例外があるという話 | ||
- 確かにわざわざ山と谷と呼ぶことで例外を想定しにくくなっている気がする | ||
- 上にあるようなtop/bottomは問題なさそう | ||
- https://github.com/goto-untrapped/Arai60/pull/59/files#r1777077060 | ||
|
||
## Step 3. Final Solution | ||
|
||
- シンプルな書き方に帰着 | ||
- 空リストの場合はエラーを返さない点に注意 | ||
|
||
```python | ||
class Solution: | ||
def maxProfit(self, prices: List[int]) -> int: | ||
profit = 0 | ||
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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. 確かにそういう考え方もありますね、ありがとうございます! |
||
return profit | ||
``` |
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.
これに気づけると楽ですよね。自分は、この手の少し算数的(貪欲法)な問題は各パターンを手書きして考えてみています。