【LeetCode】198.House Robber
来源:互联网 发布:apache一键安装包 编辑:程序博客网 时间:2024/05/16 01:13
题目:
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.
Credits:
Special thanks to @ifanchu for adding this problem and creating all test cases. Also thanks to @ts for adding additional test cases.
动态规划问题,设maxV[i]为达到第i家商店所能获得的最大利益,则maxV[i] = max(maxV[i-2] + nums[i] ,maxV[i-1]);
代码:
public class Solution { public int rob(int[] nums) {int[] maxV = new int[nums.length];if (nums.length == 0)return 0;else if (nums.length == 1) {return nums[0];} else {maxV[0] = nums[0];maxV[1] = Math.max(maxV[0], nums[1]);for (int i = 2; i < nums.length; i++) {maxV[i] = Math.max(maxV[i - 2] + nums[i], maxV[i - 1]);}}return maxV[nums.length-1];}}
- [LeetCode]198.House Robber
- 【LeetCode】198. House Robber
- 【LeetCode】198.House Robber
- LeetCode 198. House Robber
- [leetcode] 198.House Robber
- [leetcode] 198. House Robber
- LeetCode:198. House Robber
- 198. House Robber LeetCode
- [LeetCode]198. House Robber
- LeetCode || 198. House Robber
- LeetCode-198. House Robber
- [LeetCode]198. House Robber
- 【LeetCode】198. House Robber
- LeetCode#198. House Robber
- LeetCode 198. House Robber
- leetcode 198. House Robber
- LeetCode *** 198. House Robber
- leetcode 198. House Robber
- TabHost fragment 和 Viewpager 总结
- windows下安装redis
- Android中的文件读写
- appStore上传苹果应用程序软件发布流程
- 写在毕业快满一年之际
- 【LeetCode】198.House Robber
- 问题二20150511——jar包冲突(报java.lang.NoSuchMethodException)
- Xcode里-ObjC, -all_load, -force_load
- iOS 开发之利用AFNetwoking实现GET请求
- 区间动态规划
- Android Studio 自定义设置注释模板——诺诺"涂鸦"记忆
- L_Oil Deposits(HDU_1241)
- 最新资讯2015网络与信息安全博览会
- 南阳oj