198. House Robber
来源:互联网 发布:儿童编程 ipad 编辑:程序博客网 时间:2024/06/13 09:59
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 andit 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 tonightwithout alerting the police.
Credits:
Special thanks to @ifanchu for adding this problem and creating all test cases. Also thanks to @ts for adding additional test cases.
官方解答:
public int rob(int[] num) { int[][] dp = new int[num.length + 1][2]; for (int i = 1; i <= num.length; i++) { dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1]); dp[i][1] = num[i - 1] + dp[i - 1][0]; } return Math.max(dp[num.length][0], dp[num.length][1]);}
dp[i][1] means we rob the current house and dp[i][0] means we don't,
so it is easy to convert this to O(1) space
public int rob(int[] num) { int prevNo = 0; int prevYes = 0; for (int n : num) { int temp = prevNo; prevNo = Math.max(prevNo, prevYes); prevYes = n + temp; } return Math.max(prevNo, prevYes);}
- 337. House Robber III+198. House Robber
- 198. House Robber&&213. House Robber II
- 198. House Robber && 213. House Robber II
- [LeetCode]198.House Robber
- 【LeetCode】198. House Robber
- 【LeetCode】198.House Robber
- LeetCode 198. House Robber
- 198.House Robber
- [leetcode] 198.House Robber
- [leetcode] 198. House Robber
- LeetCode:198. House Robber
- 198. House Robber LeetCode
- 198. House Robber
- [LeetCode]198. House Robber
- 198. House Robber
- LeetCode || 198. House Robber
- 198. House Robber
- LeetCode-198. House Robber
- 多模块小项目~~简*学生管理系统(重点思路咯)小白撸代码第30天
- TensorFlow学习笔记
- Scratch 游戏项目学习法 —— 接苹果(完)改良你的游戏
- PAT 甲级 1118. Birds in Forest (25)
- 第一次玩CsdnBOKE
- 198. House Robber
- 如何快速生成十九大会议内容摘要
- 多项式相加减【数据结构实验报告】
- webview高度自适应图文详情
- 197. Rising Temperature
- i386和X86各是什么意思
- 归并算法的JS实现及分析(分治,递归,归并)
- 模拟
- Java动态代理