Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
- 株を持っている/いない場合の保有現金最大値を管理する方法
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DPでも(個人的に少し直感的ではないですが)解けるので、解いてみても良いかと思いました。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DP で書いてみました。私もあまり直観的ではないなと感じています。

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        cash_with_stock = -prices[0]
        cash_without_stock = 0
        for i in range(1, len(prices)):
            prev_cash_with_stock = cash_with_stock
            cash_with_stock = max(cash_with_stock, cash_without_stock - prices[i])
            cash_without_stock = max(cash_without_stock, prev_cash_with_stock + prices[i])
        return cash_without_stock

- 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)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

peakの対義語として、bottomでも良いかもしれません。
また、関数名で使われているので、そのままlowest, highestでも良いかと思いました。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The 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
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

特に問題ないと思います。
個人的には i を day に変えると、より直感的になるかと思っています。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

day は自分の選択肢にはなかったので今後は選択肢に入れるようにします。ありがとうございます。

```

2分で1回Accept (STEP1と同じため省略)