leetcode 198. House Robber(DP问题)
来源:互联网 发布:ionic lab mac百度云 编辑:程序博客网 时间:2024/05/16 17:18
问题描述:
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[n]; int[] num2 = new int[n]; 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]); }}
阅读全文
0 0
- leetcode 198. House Robber(DP问题)
- leetcode: 198. House Robber dp
- LeetCode 198. [DP]House Robber
- [leetcode][DP] House Robber
- LeetCode House Robber DP
- LeetCode House Robber || DP
- 【dp】Leetcode House Robber&& House Robber II
- LeetCode 198. House Robber【DP+滚动数组】
- 198. House Robber (dp)
- 198. House Robber (DP)
- 198. House Robber (dp)
- [leetcode][DP] House Robber II
- LeetCode 198 House Robber (dp)
- [LeetCode]198.House Robber
- 【LeetCode】198. House Robber
- 【LeetCode】198.House Robber
- LeetCode 198. House Robber
- [leetcode] 198.House Robber
- STL
- MDM命令操作介绍
- 四轴飞行器、远程视频图像传输
- AOP快速入门案例(一)
- Serverless
- leetcode 198. House Robber(DP问题)
- JAVA笔记-如何将百万级数据高效的导出到Excel表单
- 24点游戏
- [原创]Python 运行 shell 获取输出结果
- Vue 2.1.7 源码学习
- Redis-单个数据库结构---过期键
- unixODBC的使用
- 正定矩阵、二次型、合同、惯性定理、Hessian
- 用Zookeeper作为Spring cloud的配置中心