Skip to content
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions 122_best_time_to_buy_and_sell_stock_ii/problem.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
## 問題: [122. Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/)
19 changes: 19 additions & 0 deletions 122_best_time_to_buy_and_sell_stock_ii/step1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Step 1

- 各整数を折れ線グラフの点と考え、右上がりになるものを合計に加えていく
- 上記を実現する式: $maxProfit=maxProfit + max(prices[i] - prices[i-1], 0)$

```python
class Solution:
def maxProfit(self, prices: List[int]) -> int:
if len(prices) <= 1:
return 0
max_profit = 0
for i in range(1, len(prices)):
max_profit += max(prices[i] - prices[i - 1], 0)
return max_profit
```

時間計算量: $O(n)$

空間計算量: $O(1)$
16 changes: 16 additions & 0 deletions 122_best_time_to_buy_and_sell_stock_ii/step2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Step 2

```python
class Solution:
def maxProfit(self, prices: List[int]) -> int:
if len(prices) < 2:
return 0
max_profit = 0
for i in range(1, len(prices)):
max_profit += max(prices[i] - prices[i - 1], 0)
return max_profit
```

時間計算量: $O(n)$

空間計算量: $O(1)$
17 changes: 17 additions & 0 deletions 122_best_time_to_buy_and_sell_stock_ii/step3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Step 3

```python
class Solution:
def maxProfit(self, prices: List[int]) -> int:
if len(prices) < 2:
return 0
max_profit = 0
for i in range(1, len(prices)):
max_profit += max(prices[i] - prices[i - 1], 0)
return max_profit
```
1回目: 0分 56秒

2回目: 0分 48秒

3回目: 0 53秒