[题目] 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.
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.
[分析]
一维动态规划,记num[i]对应的最大安全盗金为safe[i], 递推式为safe[i] = max(safe[i-1], safe[i-2] + num[i])。
实现时仅需一个三元数组,循环使用.
// version1: 思路不够清晰准确,递推式写错但撞大运实现ok,可读性不好的初版 // 后追加注释 public class Solution { public int rob(int[] num) { if (num == null || num.length == 0) return 0; int safe = 0; // num[i-2] 处能获取的最大盗金,num[i-2]本身不一定被盗 int adj = 0; // num[i-1]能获取的最大盗金,num[i-1]本身不一定被盗 int curr = 0; // 盗取num[i]所获的最大盗金 int max = 0; // num[i]处能获取的最大盗金=max(curr, adj) for (int i = 0; i < num.length; i++) { curr = safe + num[i]; if (curr > max) max = curr; safe = adj; adj = max; } return max; } }
// version 2 // 递推式:safe[i] = max(safe[i-1], safe[i-2] + num[i]) // safe[2]表示num[i]处的最多盗金 // safe[1]表示num[i-1],即在与num[i]相邻的前面一户能拿的最大盗金 // safe[0]表示num[i-2]处最大盗金 public class Solution { public int rob(int[] num) { if (num == null || num.length == 0) return 0; int n = num.length; if (n == 1) return num[0]; else if (n == 2) return num[0] > num[1] ? num[0] : num[1]; int[] safe = {num[0], (num[1] > num[0]? num[1] : num[0]), 0}; for (int i = 2; i < num.length; i++) { safe[2] = Math.max(safe[1], safe[0] + num[i]); safe[0] = safe[1]; safe[1] = safe[2]; } return safe[2]; } }
相关推荐
HouseRobber-问题-LeetCode- @Faizansayeed28 代码 /** * 问题陈述- 你是一名职业劫匪,计划抢劫街道上的房屋。 每个房子都有一定数量的钱 藏起来,阻止你抢劫他们的唯一限制是相邻的房子有安全系统 连接,如果同一...
House Robber [198]6. Range Sum Query - Immutable [303]7. Counting Bits [338]8. Palindromic Substrings [647]9. Maximum Length of Pair Chain [646]10. Integer Break [343]11. Count Numbers with Unique ...
leetcode 和 oj OJ_Solution Solutions for OJ such as leetcode, poj and so on 版本 ...https://leetcode.com/problems/two-sum/ ...java/houserobber https://leetcode.com/problems/house-robber/
lru cache leetcode LeetCode copy and think 加油学习 1. 时间轴 时间 题目 描述 知识点 ...House Robber 动态规划 如何确定子问题 20200525 20200525 20200525 20200525 20200525 20200525 $1 234
House Robber 学会了数组的创建,以及动态规划最基本的题目 2021.2.2 322 兑换钱币 学会了 Arrays.fill 的使用,以及查看源码,返回 记录在哔哩哔哩 中 2021.2.3 53 最大子数字之和 先更新max,再将负数赋值为0 62 ...
leetcode 【演示记录】 报告 展示 2017/03/06 1.二和,167.二和二 2107/03/06 15.3 总和,16.3 总和最近,18.4 总和,11.最多水的容器 2017/03/09 62.Unique Paths, 63.Unique Paths II, 64.Minimum Path Sum 2017/...
lru cache leetcode LeetCode 解决题目的总数: 136/1753 微信公众号: 工程师Ruojhen 算法-动态规划 题号 名称 English Name 题解 53 最大子序和 Maximum Subarray 70 ...House Robber 213 打家劫舍Ⅱ
HouseRobber(#198), ClimbingStairs(#70), CoinChange(#322), EditDistance(#72) 图 : 集合的合并(直接求并、按大小求并和按深度求并)、根搜寻(路径压缩) : 有向图和无向图的邻接表表示方法 : 图的深度优先和广度...
编码实践 编码实践 模式:二进制搜索: 二进制搜索 二进制搜索: : 排序数组的上限: : ... 众议院强盗2: ://leetcode.com/problems/house-robber-ii/discuss/59921/9-lines-0ms-O(1)-Space-C++-sol
House Robber III Range Sum Query - Immutable Range Sum Query 2D - Immutable 图 Clone Graph 位操作 Reverse Bits Repeated DNA Sequences Number of 1 Bits Gray Code Single Number Single Number II Single ...