题目描述
一个专业的小偷,计划偷窃沿街的房屋。每间房内都藏有一定的现金,影响小偷偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警。
给定一个代表每个房屋存放金额的非负整数数组 nums
,请计算 不触动警报装置的情况下 ,一夜之内能够偷窃到的最高金额。
示例 1:
输入:nums = [1,2,3,1]
输出:4
解释:偷窃 1 号房屋 (金额 = 1) ,然后偷窃 3 号房屋 (金额 = 3)。
偷窃到的最高金额 = 1 + 3 = 4 。
示例 2:
输入:nums = [2,7,9,3,1]
输出:12
解释:偷窃 1 号房屋 (金额 = 2), 偷窃 3 号房屋 (金额 = 9),接着偷窃 5 号房屋 (金额 = 1)。
偷窃到的最高金额 = 2 + 9 + 1 = 12 。
提示:
1 <= nums.length <= 100
0 <= nums[i] <= 400
注意:本题与主站 198 题相同: https://leetcode.cn/problems/house-robber/
解法
方法一:动态规划
我们定义 \(f[i]\) 表示前 \(i\) 间房屋能偷窃到的最高总金额,初始时 \(f[0]=0\), \(f[1]=nums[0]\)。
考虑 \(i \gt 1\) 的情况,第 \(i\) 间房屋有两个选项:
- 不偷窃第 \(i\) 间房屋,偷窃总金额为 \(f[i-1]\);
- 偷窃第 \(i\) 间房屋,偷窃总金额为 \(f[i-2]+nums[i-1]\);
因此,我们可以得到状态转移方程:
\[
f[i]=
\begin{cases}
0, & i=0 \\
nums[0], & i=1 \\
\max(f[i-1],f[i-2]+nums[i-1]), & i \gt 1
\end{cases}
\]
最终的答案即为 \(f[n]\),其中 \(n\) 是数组的长度。
时间复杂度 \(O(n)\),空间复杂度 \(O(n)\)。其中 \(n\) 是数组长度。
| class Solution:
def rob(self, nums: List[int]) -> int:
n = len(nums)
f = [0] * (n + 1)
f[1] = nums[0]
for i in range(2, n + 1):
f[i] = max(f[i - 1], f[i - 2] + nums[i - 1])
return f[n]
|
| class Solution {
public int rob(int[] nums) {
int n = nums.length;
int[] f = new int[n + 1];
f[1] = nums[0];
for (int i = 2; i <= n; ++i) {
f[i] = Math.max(f[i - 1], f[i - 2] + nums[i - 1]);
}
return f[n];
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13 | class Solution {
public:
int rob(vector<int>& nums) {
int n = nums.size();
int f[n + 1];
memset(f, 0, sizeof(f));
f[1] = nums[0];
for (int i = 2; i <= n; ++i) {
f[i] = max(f[i - 1], f[i - 2] + nums[i - 1]);
}
return f[n];
}
};
|
| func rob(nums []int) int {
n := len(nums)
f := make([]int, n+1)
f[1] = nums[0]
for i := 2; i <= n; i++ {
f[i] = max(f[i-1], f[i-2]+nums[i-1])
}
return f[n]
}
|
| function rob(nums: number[]): number {
const n = nums.length;
const f: number[] = Array(n + 1).fill(0);
f[1] = nums[0];
for (let i = 2; i <= n; ++i) {
f[i] = Math.max(f[i - 1], f[i - 2] + nums[i - 1]);
}
return f[n];
}
|
| impl Solution {
pub fn rob(nums: Vec<i32>) -> i32 {
let mut f = [0, 0];
for x in nums {
f = [f[0].max(f[1]), f[0] + x];
}
f[0].max(f[1])
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 | class Solution {
func rob(_ nums: [Int]) -> Int {
let n = nums.count
if n == 0 { return 0 }
if n == 1 { return nums[0] }
var f = Array(repeating: 0, count: n + 1)
f[1] = nums[0]
for i in 2...n {
f[i] = max(f[i - 1], f[i - 2] + nums[i - 1])
}
return f[n]
}
}
|
方法二:动态规划(空间优化)
我们注意到,当 \(i \gt 2\) 时,\(f[i]\) 只和 \(f[i-1]\) 与 \(f[i-2]\) 有关,因此我们可以使用两个变量代替数组,将空间复杂度降到 \(O(1)\)。
| class Solution:
def rob(self, nums: List[int]) -> int:
f = g = 0
for x in nums:
f, g = max(f, g), f + x
return max(f, g)
|
| class Solution {
public int rob(int[] nums) {
int f = 0, g = 0;
for (int x : nums) {
int ff = Math.max(f, g);
g = f + x;
f = ff;
}
return Math.max(f, g);
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12 | class Solution {
public:
int rob(vector<int>& nums) {
int f = 0, g = 0;
for (int& x : nums) {
int ff = max(f, g);
g = f + x;
f = ff;
}
return max(f, g);
}
};
|
| func rob(nums []int) int {
f, g := 0, 0
for _, x := range nums {
f, g = max(f, g), f+x
}
return max(f, g)
}
|
| function rob(nums: number[]): number {
let [f, g] = [0, 0];
for (const x of nums) {
[f, g] = [Math.max(f, g), f + x];
}
return Math.max(f, g);
}
|