Skip to content

Commit 30772f6

Browse files
feat(translation): house robber (#394)
Co-authored-by: lucifer <azl397985856@gmail.com>
1 parent 10c0d9b commit 30772f6

File tree

2 files changed

+182
-0
lines changed

2 files changed

+182
-0
lines changed

README.en.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ The data structures mainly include:
102102

103103
- [0053.maximum-sum-subarray](./problems/53.maximum-sum-subarray-en.md) 🆕
104104

105+
- [0198.house-robber](./problems/198.house-robber.en.md)🆕
106+
105107
#### Medium (Translation in Progress)
106108

107109
- [0002. Add Two Numbers](./problems/2.add-two-numbers.en.md)

problems/198.house-robber.en.md

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
## House Robber
2+
3+
https://leetcode.com/problems/house-robber/description/
4+
5+
## Problem Description
6+
7+
```
8+
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
9+
10+
Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.
11+
12+
Example 1:
13+
14+
Input: [1,2,3,1]
15+
Output: 4
16+
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
17+
Total amount you can rob = 1 + 3 = 4.
18+
Example 2:
19+
20+
Input: [2,7,9,3,1]
21+
Output: 12
22+
Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1).
23+
Total amount you can rob = 2 + 9 + 1 = 12.
24+
25+
```
26+
27+
## Prerequisites
28+
29+
- Dynamic Programming
30+
31+
## Solution
32+
33+
This is a simple and classical dynamic programming problem, but it's helpful for understanding dynamic programming.
34+
35+
Frequent questions when it comes to DP problems involve:
36+
1. Why the solution works like that? Why it works without any DP array?
37+
2. Why we can apply the idea of Fibonacci Numbers to the Climbing Stairs problem?
38+
39+
We will take a look at these problems here. Similar to other DP problems, we are essentially deciding whether rob the i<sup>th</sup> house or not.
40+
41+
If we rob the i<sup>th</sup> house, the gain would be `nums[i] + dp[i - 2]`.
42+
> We cannot rob i-1<sup>th</sup> house, otherwise the alarm will be triggered.
43+
44+
If we do not rob the i<sup>th</sup> house, the gain would be `dp[i - 1]`.
45+
> The dp here is our subproblem.
46+
47+
Since we always want a larger gain, it's easy to obtain the transition formula: `dp[i] = Math.max(dp[i - 2] + nums[i - 2], dp[i - 1]);`
48+
> Note: For the convenience of calculation, we set both dp[0] and dp[1] to be 0. This way, dp[i] is actually for the i-1<sup>th</sup> house.
49+
50+
We can use the following graph to illustrate the above process:
51+
![198.house-robber](../assets/problems/198.house-robber.png)
52+
53+
If we optimize it further, we only need dp[i - 1] and dp[i - 2] when determining each dp[i]. For example, to calculate dp[6], we would only need dp[5] and dp[4], and there's no need to keep dp[3], dp[2], and so on, in memory.
54+
55+
Then, the code will be:
56+
57+
```js
58+
let a = 0;
59+
let b = 0;
60+
61+
for (let i = 0; i < nums.length; i++) {
62+
const temp = b;
63+
b = Math.max(a + nums[i], b);
64+
a = temp;
65+
}
66+
67+
return b;
68+
```
69+
70+
The above code optimized the space complexity from O(n) to O(1). The same optimization applies to a lot of DP problems.
71+
72+
## Key Points
73+
74+
## Code (JS/C++/Python)
75+
76+
JavaScript Code:
77+
78+
```js
79+
/*
80+
* @lc app=leetcode id=198 lang=javascript
81+
*
82+
* [198] House Robber
83+
*
84+
* https://leetcode.com/problems/house-robber/description/
85+
*
86+
* algorithms
87+
* Easy (40.80%)
88+
* Total Accepted: 312.1K
89+
* Total Submissions: 762.4K
90+
* Testcase Example: '[1,2,3,1]'
91+
*
92+
* You are a professional robber planning to rob houses along a street. Each
93+
* house has a certain amount of money stashed, the only constraint stopping
94+
* you from robbing each of them is that adjacent houses have security system
95+
* connected and it will automatically contact the police if two adjacent
96+
* houses were broken into on the same night.
97+
*
98+
* Given a list of non-negative integers representing the amount of money of
99+
* each house, determine the maximum amount of money you can rob tonight
100+
* without alerting the police.
101+
*
102+
* Example 1:
103+
*
104+
*
105+
* Input: [1,2,3,1]
106+
* Output: 4
107+
* Explanation: Rob house 1 (money = 1) and then rob house 3 (money =
108+
* 3).
109+
* Total amount you can rob = 1 + 3 = 4.
110+
*
111+
* Example 2:
112+
*
113+
*
114+
* Input: [2,7,9,3,1]
115+
* Output: 12
116+
* Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house
117+
* 5 (money = 1).
118+
* Total amount you can rob = 2 + 9 + 1 = 12.
119+
*
120+
*
121+
*/
122+
/**
123+
* @param {number[]} nums
124+
* @return {number}
125+
*/
126+
var rob = function(nums) {
127+
// Tag: DP
128+
const dp = [];
129+
dp[0] = 0;
130+
dp[1] = 0;
131+
132+
for (let i = 2; i < nums.length + 2; i++) {
133+
dp[i] = Math.max(dp[i - 2] + nums[i - 2], dp[i - 1]);
134+
}
135+
136+
return dp[nums.length + 1];
137+
};
138+
```
139+
140+
C++ Code:
141+
142+
> This is slightly different from the JavaScript solution, but it uses the same transition function.
143+
144+
```C++
145+
class Solution {
146+
public:
147+
int rob(vector<int>& nums) {
148+
if (nums.empty()) return 0;
149+
auto sz = nums.size();
150+
if (sz == 1) return nums[0];
151+
auto prev = nums[0];
152+
auto cur = max(prev, nums[1]);
153+
for (auto i = 2; i < sz; ++i) {
154+
auto tmp = cur;
155+
cur = max(nums[i] + prev, cur);
156+
prev = tmp;
157+
}
158+
return cur;
159+
}
160+
};
161+
```
162+
163+
Python Code:
164+
165+
```python
166+
class Solution:
167+
def rob(self, nums: List[int]) -> int:
168+
if not nums:
169+
return 0
170+
171+
length = len(nums)
172+
if length == 1:
173+
return nums[0]
174+
else:
175+
prev = nums[0]
176+
cur = max(prev, nums[1])
177+
for i in range(2, length):
178+
cur, prev = max(prev + nums[i], cur), cur
179+
return cur
180+
```

0 commit comments

Comments
 (0)