[LeetCode]House Robber
来源:互联网 发布:盛势网络剧08bilibili 编辑:程序博客网 时间:2024/05/28 05: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.
题意:在一条街上,有很多房子,房子里面有money,不能抢相连的房子,求抢到最大money。
题解:采用动态规划,如果当前在i为止,dp[i] = max(dp[i-2]+money[i],dp[i-1]), i=0 ,dp[0]=money[0],dp[1] = max(money[0],money[1]);
code:
public int rob(int[] nums) { if(nums == null || nums.length == 0){ return 0; } int[] dp = new int[nums.length];dp[0] = nums[0];if(nums.length>1){ dp[1] = Math.max(nums[0], nums[1]); for(int i=2; i<nums.length; i++){ dp[i] =Math.max(dp[i-2]+nums[i], dp[i-1]); }}return dp[nums.length-1]; }
0 0
- 【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题解
- leetcode:House Robber
- [LeetCode]House Robber
- BAT批处理中的字符串处理详解(字符串截取),bat批处理
- 我的thinkPHP入门
- mina 框架java服务端的搭建和通信。
- Spring MVC How take the parameter value of a GET HTTP Request in my controller method?
- 一千行MySQL学习笔记(五)
- [LeetCode]House Robber
- Flask-SQLALchemy学习笔记
- QT 中网络数据与本地数据的转换(Windows)
- 一只简单的网络爬虫(基于linux C/C++)————读取命令行参数及日志宏设计
- MyEclipse 快捷键(快捷键无效解决方案)
- 解决Could not reliably determine the server's fully qualified domain name
- LINUX常用命令大全
- shell 冒泡排序 数组的使用
- kingshard--一个Go开发高性能MySQL Proxy项目