198. House Robber
198. House Robber
题目
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 systems connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
Given an integer array nums
representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police.
Example 1:
Input: nums = [1,2,3,1]
Output: 4
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
Total amount you can rob = 1 + 3 = 4.
Example 2:
Input: nums = [2,7,9,3,1]
Output: 12
Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1).
Total amount you can rob = 2 + 9 + 1 = 12.
Constraints:
1 <= nums.length <= 100
0 <= nums[i] <= 400
题目大意
你是一个专业的小偷,计划偷窃沿街的房屋。每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警。
给定一个代表每个房屋存放金额的非负整数数组,计算你 不触动警报装置的情况下 ,一夜之内能够偷窃到的最高金额。
示例 1:
输入:[1,2,3,1]
输出:4
解释:偷窃 1 号房屋 (金额 = 1) ,然后偷窃 3 号房屋 (金额 = 3)。
偷窃到的最高金额 = 1 + 3 = 4 。
示例 2:
输入:[2,7,9,3,1]
输出:12
解释:偷窃 1 号房屋 (金额 = 2), 偷窃 3 号房屋 (金额 = 9),接着偷窃 5 号房屋 (金额 = 1)。
偷窃到的最高金额 = 2 + 9 + 1 = 12 。
解题思路
这是一个经典的动态规划问题。可以使用动态规划来解决,其中的关键是定义状态和状态转移方程。
状态定义:定义一个一维数组
dp
,其中dp[i]
表示在前i
个房屋中能够打劫到的最大金额。状态转移方程:对于每个房屋,有两个选择:打劫或者不打劫。因此,状态转移方程为:
dp[i] = max(dp[i-1], dp[i-2] + nums[i])
初始化:初始化前两个状态,即
dp[0] = nums[0]
和dp[1] = Math.max(nums[0], nums[1])
。遍历计算:从第三个房屋开始遍历,根据状态转移方程更新每个状态。
结果:最终答案为
dp[n - 1]
,表示在所有房屋中能够打劫到的最大金额。
- 时间复杂度:
O(n)
- 遍历整个数组。 - 空间复杂度:
O(n)
- 使用了一个数组来存储中间状态。可以优化为O(1)
,只保留前两个状态。
代码
/**
* @param {number[]} nums
* @return {number}
*/
var rob = function (nums) {
const n = nums.length;
if (n == 0) return 0;
if (n == 1) return nums[0];
const dp = new Array(n).fill(0);
// 初始化前两个状态
dp[0] = nums[0];
dp[1] = Math.max(nums[0], nums[1]);
// 遍历计算
for (let i = 2; i < n; i++) {
dp[i] = Math.max(dp[i - 2] + nums[i], dp[i - 1]);
}
// 返回结果
return dp[n - 1];
};
/**
* @param {number[]} nums
* @return {number}
*/
var rob = function (nums) {
const n = nums.length;
if (n == 0) return 0;
if (n == 1) return nums[0];
let first = nums[0];
let second = Math.max(nums[0], nums[1]);
for (let i = 2; i < n; i++) {
let temp = Math.max(first + nums[i], second);
first = second;
second = temp;
}
return second;
};
相关题目
- [152. 乘积最大子数组](./0152.md)
- [213. 打家劫舍 II](https://leetcode.com/problems/house-robber-ii)
- [🔒 Paint House](https://leetcode.com/problems/paint-house)
- [🔒 Paint Fence](https://leetcode.com/problems/paint-fence)
- [337. 打家劫舍 III](https://leetcode.com/problems/house-robber-iii)
- [600. 不含连续 1 的非负整数](https://leetcode.com/problems/non-negative-integers-without-consecutive-ones)
- [🔒 Coin Path](https://leetcode.com/problems/coin-path)
- [740. 删除并获得点数](https://leetcode.com/problems/delete-and-earn)
- [2140. 解决智力问题](https://leetcode.com/problems/solving-questions-with-brainpower)
- [2320. 统计放置房子的方式数](https://leetcode.com/problems/count-number-of-ways-to-place-houses)
- [2560. 打家劫舍 IV](https://leetcode.com/problems/house-robber-iv)
- [2611. 老鼠和奶酪](https://leetcode.com/problems/mice-and-cheese)
- [2789. Largest Element in an Array after Merge Operations](https://leetcode.com/problems/largest-element-in-an-array-after-merge-operations)