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 213_house_robber_ii/problem.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
## 問題: [213 House Robber II](https://leetcode.com/problems/house-robber-ii/description/)
29 changes: 29 additions & 0 deletions 213_house_robber_ii/step1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Step 1

- $i$番目まで訪れた時の最大利益$maxAmount[i]$は$max(maxAmount[i - 1], maxAmount[i - 2] + nums[i])$
- 円形のため、$0\le i \le nums.size -2$の範囲の最大利益と$1\le i \le nums.size -1$の範囲の最大利益のうち大きい方を返す

```python
class Solution:
def rob(self, nums: List[int]) -> int:
if len(nums) <= 2:
return max(nums)
first_max_amount_prev_2 = nums[0]
first_max_amount_prev_1 = max(nums[0], nums[1])
for i in range(2, len(nums) - 1):
current_max_amount = max(first_max_amount_prev_1, first_max_amount_prev_2 + nums[i])
first_max_amount_prev_2 = first_max_amount_prev_1
first_max_amount_prev_1 = current_max_amount

second_max_amount_prev_2 = nums[1]
second_max_amount_prev_1 = max(nums[1], nums[2])
for i in range(3, len(nums)):
current_max_amount = max(second_max_amount_prev_1, second_max_amount_prev_2 + nums[i])
second_max_amount_prev_2 = second_max_amount_prev_1
second_max_amount_prev_1 = current_max_amount
return max(first_max_amount_prev_1, second_max_amount_prev_1)
```

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

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

```python
class Solution:
def rob(self, nums: List[int]) -> int:
if len(nums) <= 2:
return max(nums)

def rob_linear(start, end):
profit_two_back = nums[start]
profit_one_back = max(nums[start], nums[start + 1])

for i in range(start + 2, end):
current_profit = max(profit_one_back, profit_two_back + nums[i])
profit_two_back = profit_one_back
profit_one_back = current_profit

return profit_one_back

return max(rob_linear(0, len(nums) - 1), rob_linear(1, len(nums)))
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

いいと思います。


```

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

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

```python
class Solution:
def rob(self, nums: List[int]) -> int:
if len(nums) <= 2:
return max(nums)

def rob_linear(start, end):
profit_two_back = nums[start]
profit_one_back = max(nums[start], nums[start + 1])

for i in range(start + 2, end):
current_profit = max(profit_one_back, profit_two_back + nums[i])
profit_two_back = profit_one_back
profit_one_back = current_profit

return profit_one_back

return max(rob_linear(0, len(nums) - 1), rob_linear(1, len(nums)))

```

1回目: 2分 57秒

2回目: 2分 18秒

3回目: 2分 14秒