leetcode 198. House Robber(DP问题)
2017-12-20 20:01
537 查看
问题描述:
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.
思路:
利用动态规划
代码:
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.
思路:
利用动态规划
代码:
class Solution { public int rob(int[] nums) { if(nums == null || nums.length == 0) return 0; int n = nums.length; int[] num1 = new int ; int[] num2 = new int ; num1[0] = nums[0]; num2[0] = 0; for(int i = 1; i<n; i++){ num1[i] = num2[i-1] + nums[i]; num2[i] = Math.max(num1[i-1], num2[i-1]); } return Math.max(num1[n-1], num2[n-1]); } }
相关文章推荐
- LeetCode-198. House Robber (JAVA)寻找数组不相邻组合最大值DP
- 2017.12.18 LeetCode - 198. House Robber【dp入门】
- LeetCode 198. House Robber【DP+滚动数组】
- leetcode 213. House Robber II 入室抢劫 抢劫问题 + 一道经典的DP动态规划问题
- leetcode 198. House Robber
- leetcode 327. Count of Range Sum 字段和问题 + 动态规划DP解决 + 字段数 + 暴力求解真好
- 198. House Robber (dp)
- LeetCode | 746. Min Cost Climbing Stairs | 简单DP上楼梯问题
- LeetCode 198. House Robber(java)
- leetcode 53. Maximum Subarray(DP动态规划问题)
- DP问题:leetcode(96) Unique Binary Search Trees
- 动态规划解决leetcode中的House Robber问题
- LeetCode House Robber DP
- [LeetCode] 198. House Robber 打家劫舍
- LeetCode 198. House Robber
- LeetCode 198. House Robber
- LeetCode 198. House Robber(小偷游戏)
- leetcode 279. Perfect Squares 类似背包问题 + 很简单的动态规划DP解决
- DP问题:leetcode(264) Ugly Number II
- [LeetCode]198. House Robber (JavaScript)