跳至主要內容

198. 打家劫舍


198. 打家劫舍

🟠   🔖  数组 动态规划  🔗 力扣open in new window LeetCodeopen in new window

题目

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 。

解题思路

这是一个经典的动态规划问题。可以使用动态规划来解决,其中的关键是定义状态和状态转移方程。

  1. 状态定义:定义一个一维数组 dp,其中 dp[i] 表示在前 i 个房屋中能够打劫到的最大金额。

  2. 状态转移方程:对于每个房屋,有两个选择:打劫或者不打劫。因此,状态转移方程为:dp[i] = max(dp[i-1], dp[i-2] + nums[i])

  3. 初始化:初始化前两个状态,即 dp[0] = nums[0]dp[1] = Math.max(nums[0], nums[1])

  4. 遍历计算:从第三个房屋开始遍历,根据状态转移方程更新每个状态。

  5. 结果:最终答案为 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];
};

相关题目

题号标题题解标签难度力扣
152乘积最大子数组[✓]数组 动态规划🟠🀄️open in new window 🔗open in new window
213打家劫舍 II数组 动态规划🟠🀄️open in new window 🔗open in new window
256粉刷房子 🔒数组 动态规划🟠🀄️open in new window 🔗open in new window
276栅栏涂色 🔒动态规划🟠🀄️open in new window 🔗open in new window
337打家劫舍 III 深度优先搜索 动态规划 1+🟠🀄️open in new window 🔗open in new window
600不含连续1的非负整数动态规划🔴🀄️open in new window 🔗open in new window
656成本最小路径 🔒数组 动态规划🔴🀄️open in new window 🔗open in new window
740删除并获得点数数组 哈希表 动态规划🟠🀄️open in new window 🔗open in new window
2140解决智力问题数组 动态规划🟠🀄️open in new window 🔗open in new window
2320统计放置房子的方式数动态规划🟠🀄️open in new window 🔗open in new window
2560打家劫舍 IV数组 二分查找🟠🀄️open in new window 🔗open in new window
2611老鼠和奶酪贪心 数组 排序 1+🟠🀄️open in new window 🔗open in new window
2789合并后数组中的最大元素贪心 数组🟠🀄️open in new window 🔗open in new window