开始刷leetcode day3: house robber
来源:互联网 发布:mdf文件恢复数据库 编辑:程序博客网 时间:2024/05/16 15:06
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.
Java:
public class Solution {
public int rob(int[] nums) {
if(nums.length <= 1)
return nums.length == 0? 0: nums[0];
int[] best = new int[nums.length];
best[0] = nums[0];
best[1] = Math.max(nums[1],nums[0]);
for(int i=2; i< nums.length; i++)
{
best[i] = Math.max(best[i-2] + nums[i], best[i-1]);
}
return best[nums.length - 1];
}
}
这道题需要用dp的思想来解,一开始想不通,还是先看了下别人的思路。。。。此代码仅供参考
- 开始刷leetcode day3: house robber
- 开始刷leetcode day63:House Robber II
- 【dp】Leetcode House Robber&& House Robber II
- 【leetcode】House Robber && House Robber II
- LeetCode --- House Robber & House Robber II
- LeetCode OJ House Robber
- Leetcode: House Robber
- Leetcode 198: House Robber
- House Robber - LeetCode
- LeetCode 之 House Robber
- leetcode--House Robber
- [leetcode]47 House Robber
- [LeetCode] House Robber
- LeetCode :House Robber
- [leetcode] House Robber
- [leetcode]House Robber
- [LeetCode]198.House Robber
- LeetCode House Robber题解
- 复制自身程序到windows目录和系统目录下
- 欢迎使用CSDN-markdown编辑器
- java项目管理经验总结
- 将w32 dll文件以资源的形式加载,并且在内存中调用
- CVX coding
- 开始刷leetcode day3: house robber
- iOS- 传感器用法简介
- 百万宝贝观后感
- 硫磺岛家书观后感
- 开始刷leetcode day3:Rotate Array
- 非常喜欢的漫画《I'S》
- InstallAnywhere的自定义PANEL
- std map set 区别
- HMM