LeetCode 198. [DP]House Robber
来源:互联网 发布:枪花乐队知乎 编辑:程序博客网 时间:2024/06/07 22:26
题目链接
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 s[100000];//将nums[]数组复制到s[] int dfs(int idx,int n)//从idx开始抢钱 最多能够抢多少钱 { if(idx>=n)//全部抢完了 return 0; int a = s[idx] + dfs(idx+2,n);//第一个小机器人 int b = 0 + dfs(idx+1,n);//第二个小机器人 if(a>b) return a; else return b; } int rob(vector<int>& nums) { for(int i=0;i<nums.size();i++) s[i]=nums[i]; return dfs(0,nums.size()) ; }};
超时原因 有些部分被算了至少两次改进代码
class Solution {public: int s[100000];//保存价格 bool is[10000]={false};//s[idx] = true 表示算过dfs(idx,n) 不用重新计算 int f[1000]={0}; //记录 dfs(idx,n)的大小 int dfs(int idx,int n)//从idx开始抢钱 最多能够抢多少钱 { if(idx>=n)//全部抢完了 return 0; int a,b; if(!is[idx])//如果没有计算过dfs(idx,n) { is[idx] = true; a = s[idx] + dfs(idx+2,n);//idx房子偷,下次从idx+2开始偷 b = 0 + dfs(idx+1,n);//idx不偷,下次从idx+1开始偷 if(a>b) f[idx]=a;//idx位置偷与不偷最大值 else f[idx]=b; } else return f[idx];//以前计算过dfs(idx,n) 直接从f[idx]找出 return f[idx]; } int rob(vector<int>& nums) { for(int i=0;i<nums.size();i++)//转换成全局变量 s[i]=nums[i]; return dfs(0,nums.size()) ; }};
自底向上算法
class Solution { public: int f[1000]={0}; //记录 dfs(idx,n)的大小 int max(int a,int b) {return a>b?a:b;} int rob(vector<int>& nums) { if(nums.size() == 0) return 0; if(nums.size() == 1) return nums[0]; if(nums.size() == 2 ) return max(nums[0],nums[1]); else { f[0] = nums[0]; f[1] = max(nums[0],nums[1]);//f[1]表示nums[0]与nums[1]中最大的一个 int i; for( i=2;i < nums.size();i++) { f[i]=max(nums[i]+f[i-2],f[i-1]); } return f[nums.size()-1]; } } };
阅读全文
0 0
- 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+滚动数组】
- 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
- Linux platform
- 数组的简单解析
- $.format
- 抽签
- spring常出现的异常
- LeetCode 198. [DP]House Robber
- 在507干活的第5天
- java集合--List总结(LinkedList, ArrayList等使用场景和性能分析)
- 在页面上获取url中的参数
- UNIX编程专题-wait和waitpid详解2
- JS_对象创建与访问
- 使用NanoHTTPD,返回值乱码问题
- 查看端口号占用
- 数据库性能优化的五种方案(mycat,基于阿里coba开源的数据库中间件,很容易实现分库分表、主从切换功能。另一个当当网开源的一个库 sharding-jdbc)