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
95 changes: 95 additions & 0 deletions Python3/122. Best Time to Buy and Sell Stock II.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
## Step 1. Initial Solution

- ある点までの利益とその後の利益を足す?
- 何回も足す必要があるしO(n)を何回かやることになる
- よく考えると上がるところを全部計上すれば良いだけ
Copy link

@Mike0121 Mike0121 Aug 24, 2025

Choose a reason for hiding this comment

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

これに気づけると楽ですよね。自分は、この手の少し算数的(貪欲法)な問題は各パターンを手書きして考えてみています。

- これで実装して以下の通り
- 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]

Choose a reason for hiding this comment

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

変数名、buyは、動詞単体のため少しわかりづらいと感じました。buy_priceなどはどうでしょうか?

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

Choose a reason for hiding this comment

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

いいと思いました。
完全に趣味ですが、price_diffよりはprofitの方が好みでした。差に関する変数だという意味にプラスして、当日-前日なのか、前日-当日なのかが伝わる気がするので。

Copy link
Owner Author

Choose a reason for hiding this comment

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

確かにそういう考え方もありますね、ありがとうございます!

return profit
```