Skip to content

Commit cbdf232

Browse files
authored
feat: add solutions for lc No.3840 (#5052)
1 parent 2602b80 commit cbdf232

8 files changed

Lines changed: 336 additions & 8 deletions

File tree

solution/3800-3899/3840.House Robber V/README.md

Lines changed: 119 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,32 +92,147 @@ tags:
9292

9393
<!-- solution:start -->
9494

95-
### 方法一
95+
### 方法一:动态规划
96+
97+
我们定义两个变量 $f$ 和 $g$,其中 $f$ 表示当前房屋不被偷窃时的最大金额,而 $g$ 表示当前房屋被偷窃时的最大金额。初始时 $f = 0$, $g = nums[0]$。答案为 $\max(f, g)$。
98+
99+
接下来,我们从第二间房屋开始遍历:
100+
101+
- 如果当前房屋与前一间房屋颜色相同,则 $f$ 的值为 $\max(f, g)$,而 $g$ 的值为 $f + nums[i]$。
102+
- 如果当前房屋与前一间房屋颜色不同,则 $f$ 的值为 $\max(f, g)$,而 $g$ 的值为 $\max(f, g) + nums[i]$。
103+
104+
最后返回 $\max(f, g)$ 即可。
105+
106+
时间复杂度 $O(n)$,其中 $n$ 是房屋的数量。空间复杂度 $O(1)$。
96107

97108
<!-- tabs:start -->
98109

99110
#### Python3
100111

101112
```python
102-
113+
class Solution:
114+
def rob(self, nums: List[int], colors: List[int]) -> int:
115+
n = len(nums)
116+
f, g = 0, nums[0]
117+
for i in range(1, n):
118+
if colors[i - 1] == colors[i]:
119+
f, g = max(f, g), f + nums[i]
120+
else:
121+
f, g = max(f, g), max(f, g) + nums[i]
122+
return max(f, g)
103123
```
104124

105125
#### Java
106126

107127
```java
108-
128+
class Solution {
129+
public long rob(int[] nums, int[] colors) {
130+
int n = nums.length;
131+
long f = 0, g = nums[0];
132+
for (int i = 1; i < n; i++) {
133+
if (colors[i - 1] == colors[i]) {
134+
long gg = f + nums[i];
135+
f = Math.max(f, g);
136+
g = gg;
137+
} else {
138+
long gg = Math.max(f, g) + nums[i];
139+
f = Math.max(f, g);
140+
g = gg;
141+
}
142+
}
143+
return Math.max(f, g);
144+
}
145+
}
109146
```
110147

111148
#### C++
112149

113150
```cpp
114-
151+
class Solution {
152+
public:
153+
long long rob(vector<int>& nums, vector<int>& colors) {
154+
int n = nums.size();
155+
long long f = 0, g = nums[0];
156+
for (int i = 1; i < n; i++) {
157+
if (colors[i - 1] == colors[i]) {
158+
long long gg = f + nums[i];
159+
f = max(f, g);
160+
g = gg;
161+
} else {
162+
long long gg = max(f, g) + nums[i];
163+
f = max(f, g);
164+
g = gg;
165+
}
166+
}
167+
return max(f, g);
168+
}
169+
};
115170
```
116171
117172
#### Go
118173
119174
```go
175+
func rob(nums []int, colors []int) int64 {
176+
n := len(nums)
177+
var f int64 = 0
178+
var g int64 = int64(nums[0])
179+
180+
for i := 1; i < n; i++ {
181+
if colors[i-1] == colors[i] {
182+
f, g = max(f, g), f+int64(nums[i])
183+
} else {
184+
f, g = max(f, g), max(f, g)+int64(nums[i])
185+
}
186+
}
187+
188+
return max(f, g)
189+
}
190+
```
191+
192+
#### TypeScript
193+
194+
```ts
195+
function rob(nums: number[], colors: number[]): number {
196+
const n = nums.length;
197+
let f = 0;
198+
let g = nums[0];
199+
200+
for (let i = 1; i < n; i++) {
201+
if (colors[i - 1] === colors[i]) {
202+
[f, g] = [Math.max(f, g), f + nums[i]];
203+
} else {
204+
[f, g] = [Math.max(f, g), Math.max(f, g) + nums[i]];
205+
}
206+
}
207+
208+
return Math.max(f, g);
209+
}
210+
```
120211

212+
#### Rust
213+
214+
```rust
215+
impl Solution {
216+
pub fn rob(nums: Vec<i32>, colors: Vec<i32>) -> i64 {
217+
let n = nums.len();
218+
let mut f: i64 = 0;
219+
let mut g: i64 = nums[0] as i64;
220+
221+
for i in 1..n {
222+
if colors[i - 1] == colors[i] {
223+
let gg = f + nums[i] as i64;
224+
f = f.max(g);
225+
g = gg;
226+
} else {
227+
let gg = f.max(g) + nums[i] as i64;
228+
f = f.max(g);
229+
g = gg;
230+
}
231+
}
232+
233+
f.max(g)
234+
}
235+
}
121236
```
122237

123238
<!-- tabs:end -->

solution/3800-3899/3840.House Robber V/README_EN.md

Lines changed: 119 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,32 +89,147 @@ tags:
8989

9090
<!-- solution:start -->
9191

92-
### Solution 1
92+
### Solution 1: Dynamic Programming
93+
94+
We define two variables $f$ and $g$, where $f$ represents the maximum amount when the current house is not robbed, and $g$ represents the maximum amount when the current house is robbed. Initially, $f = 0$ and $g = nums[0]$. The answer is $\max(f, g)$.
95+
96+
Next, we traverse starting from the second house:
97+
98+
- If the current house has the same color as the previous house, then $f$ is updated to $\max(f, g)$, and $g$ is updated to $f + nums[i]$.
99+
- If the current house has a different color from the previous house, then $f$ is updated to $\max(f, g)$, and $g$ is updated to $\max(f, g) + nums[i]$.
100+
101+
Finally, return $\max(f, g)$.
102+
103+
The time complexity is $O(n)$, where $n$ is the number of houses. The space complexity is $O(1)$.
93104

94105
<!-- tabs:start -->
95106

96107
#### Python3
97108

98109
```python
99-
110+
class Solution:
111+
def rob(self, nums: List[int], colors: List[int]) -> int:
112+
n = len(nums)
113+
f, g = 0, nums[0]
114+
for i in range(1, n):
115+
if colors[i - 1] == colors[i]:
116+
f, g = max(f, g), f + nums[i]
117+
else:
118+
f, g = max(f, g), max(f, g) + nums[i]
119+
return max(f, g)
100120
```
101121

102122
#### Java
103123

104124
```java
105-
125+
class Solution {
126+
public long rob(int[] nums, int[] colors) {
127+
int n = nums.length;
128+
long f = 0, g = nums[0];
129+
for (int i = 1; i < n; i++) {
130+
if (colors[i - 1] == colors[i]) {
131+
long gg = f + nums[i];
132+
f = Math.max(f, g);
133+
g = gg;
134+
} else {
135+
long gg = Math.max(f, g) + nums[i];
136+
f = Math.max(f, g);
137+
g = gg;
138+
}
139+
}
140+
return Math.max(f, g);
141+
}
142+
}
106143
```
107144

108145
#### C++
109146

110147
```cpp
111-
148+
class Solution {
149+
public:
150+
long long rob(vector<int>& nums, vector<int>& colors) {
151+
int n = nums.size();
152+
long long f = 0, g = nums[0];
153+
for (int i = 1; i < n; i++) {
154+
if (colors[i - 1] == colors[i]) {
155+
long long gg = f + nums[i];
156+
f = max(f, g);
157+
g = gg;
158+
} else {
159+
long long gg = max(f, g) + nums[i];
160+
f = max(f, g);
161+
g = gg;
162+
}
163+
}
164+
return max(f, g);
165+
}
166+
};
112167
```
113168
114169
#### Go
115170
116171
```go
172+
func rob(nums []int, colors []int) int64 {
173+
n := len(nums)
174+
var f int64 = 0
175+
var g int64 = int64(nums[0])
176+
177+
for i := 1; i < n; i++ {
178+
if colors[i-1] == colors[i] {
179+
f, g = max(f, g), f+int64(nums[i])
180+
} else {
181+
f, g = max(f, g), max(f, g)+int64(nums[i])
182+
}
183+
}
184+
185+
return max(f, g)
186+
}
187+
```
188+
189+
#### TypeScript
190+
191+
```ts
192+
function rob(nums: number[], colors: number[]): number {
193+
const n = nums.length;
194+
let f = 0;
195+
let g = nums[0];
196+
197+
for (let i = 1; i < n; i++) {
198+
if (colors[i - 1] === colors[i]) {
199+
[f, g] = [Math.max(f, g), f + nums[i]];
200+
} else {
201+
[f, g] = [Math.max(f, g), Math.max(f, g) + nums[i]];
202+
}
203+
}
204+
205+
return Math.max(f, g);
206+
}
207+
```
117208

209+
#### Rust
210+
211+
```rust
212+
impl Solution {
213+
pub fn rob(nums: Vec<i32>, colors: Vec<i32>) -> i64 {
214+
let n = nums.len();
215+
let mut f: i64 = 0;
216+
let mut g: i64 = nums[0] as i64;
217+
218+
for i in 1..n {
219+
if colors[i - 1] == colors[i] {
220+
let gg = f + nums[i] as i64;
221+
f = f.max(g);
222+
g = gg;
223+
} else {
224+
let gg = f.max(g) + nums[i] as i64;
225+
f = f.max(g);
226+
g = gg;
227+
}
228+
}
229+
230+
f.max(g)
231+
}
232+
}
118233
```
119234

120235
<!-- tabs:end -->
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
long long rob(vector<int>& nums, vector<int>& colors) {
4+
int n = nums.size();
5+
long long f = 0, g = nums[0];
6+
for (int i = 1; i < n; i++) {
7+
if (colors[i - 1] == colors[i]) {
8+
long long gg = f + nums[i];
9+
f = max(f, g);
10+
g = gg;
11+
} else {
12+
long long gg = max(f, g) + nums[i];
13+
f = max(f, g);
14+
g = gg;
15+
}
16+
}
17+
return max(f, g);
18+
}
19+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
func rob(nums []int, colors []int) int64 {
2+
n := len(nums)
3+
var f int64 = 0
4+
var g int64 = int64(nums[0])
5+
6+
for i := 1; i < n; i++ {
7+
if colors[i-1] == colors[i] {
8+
f, g = max(f, g), f+int64(nums[i])
9+
} else {
10+
f, g = max(f, g), max(f, g)+int64(nums[i])
11+
}
12+
}
13+
14+
return max(f, g)
15+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public long rob(int[] nums, int[] colors) {
3+
int n = nums.length;
4+
long f = 0, g = nums[0];
5+
for (int i = 1; i < n; i++) {
6+
if (colors[i - 1] == colors[i]) {
7+
long gg = f + nums[i];
8+
f = Math.max(f, g);
9+
g = gg;
10+
} else {
11+
long gg = Math.max(f, g) + nums[i];
12+
f = Math.max(f, g);
13+
g = gg;
14+
}
15+
}
16+
return Math.max(f, g);
17+
}
18+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def rob(self, nums: List[int], colors: List[int]) -> int:
3+
n = len(nums)
4+
f, g = 0, nums[0]
5+
for i in range(1, n):
6+
if colors[i - 1] == colors[i]:
7+
f, g = max(f, g), f + nums[i]
8+
else:
9+
f, g = max(f, g), max(f, g) + nums[i]
10+
return max(f, g)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
impl Solution {
2+
pub fn rob(nums: Vec<i32>, colors: Vec<i32>) -> i64 {
3+
let n = nums.len();
4+
let mut f: i64 = 0;
5+
let mut g: i64 = nums[0] as i64;
6+
7+
for i in 1..n {
8+
if colors[i - 1] == colors[i] {
9+
let gg = f + nums[i] as i64;
10+
f = f.max(g);
11+
g = gg;
12+
} else {
13+
let gg = f.max(g) + nums[i] as i64;
14+
f = f.max(g);
15+
g = gg;
16+
}
17+
}
18+
19+
f.max(g)
20+
}
21+
}

0 commit comments

Comments
 (0)