198. 213. 337. House Robber -- 不取相邻值的最大值
198. House Robber
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(vector<int>& nums) {
- int n = nums.size(), i;
- if( == n)
- return ;
- if( == n)
- return nums[];
- vector<int> dp(n);
- dp[] = nums[];
- dp[] = max(nums[], nums[]);
- for(i = ; i < n; i++)
- {
- dp[i] = max(dp[i-], dp[i-] + nums[i]);
- }
- return dp[n-];
- }
- };
213. House Robber II
After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, the security system for these houses remain the same as for those in the previous street.
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 orginal_rob(vector<int> &money, int start, int end) {
- int n2=;
- int n1=;
- for (int i=start; i<end; i++){
- int current = max(n1, n2 + money[i]);
- n2 = n1;
- n1 = current;
- }
- return n1;
- }
- int rob(vector<int>& nums) {
- int n = nums.size();
- switch (n) {
- case :
- return ;
- case :
- return nums[];
- case :
- return max(nums[], nums[]);
- default:
- /*
- * the idea is we cannot rob[0] and rob[n-1] at same time
- * so, we rob [0 .. n-2] or [1 .. n-1], can return the maxinum one.
- */
- int m1 = orginal_rob(nums, , n-);
- int m2 = orginal_rob(nums, , n);
- return max(m1, m2);
- }
- }
- };
337. House Robber III
The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "root." Besides the root, each house has one and only one parent house. After a tour, the smart thief realized that "all houses in this place forms a binary tree". It will automatically contact the police if two directly-linked houses were broken into on the same night.
Determine the maximum amount of money the thief can rob tonight without alerting the police.
Example 1:
- 3
- / \
- 2 3
- \ \
- 3 1
Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.
Example 2:
- 3
- / \
- 4 5
- / \ \
- 1 3 1
Maximum amount of money the thief can rob = 4 + 5 = 9.
- /**
- * Definition for a binary tree node.
- * struct TreeNode {
- * int val;
- * TreeNode *left;
- * TreeNode *right;
- * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
- * };
- */
- class Solution {
- map<TreeNode*, int> m;
- public:
- int rob(TreeNode* root) {
- if(root == NULL)
- return ;
- if(m.find(root) != m.end())
- return m[root];
- int left = rob(root->left);
- int right = rob(root->right);
- int child = left + right;
- int ans = root->val;
- if(root->left)
- {
- ans += rob(root->left->left) + rob(root->left->right);
- }
- if(root->right)
- {
- ans += rob(root->right->left) + rob(root->right->right);
- }
- m[root] = max(ans, child);
- return m[root];
- }
- };
198. 213. 337. House Robber -- 不取相邻值的最大值的更多相关文章
- (leetcode:选择不相邻元素,求和最大问题):打家劫舍(DP:198/213/337)
题型:从数组中选择不相邻元素,求和最大 (1)对于数组中的每个元素,都存在两种可能性:(1)选择(2)不选择,所以对于这类问题,暴力方法(递归思路)的时间复杂度为:O(2^n): (2)递归思路中往往 ...
- leetcode 198. House Robber 、 213. House Robber II 、337. House Robber III 、256. Paint House(lintcode 515) 、265. Paint House II(lintcode 516) 、276. Paint Fence(lintcode 514)
House Robber:不能相邻,求能获得的最大值 House Robber II:不能相邻且第一个和最后一个不能同时取,求能获得的最大值 House Robber III:二叉树下的不能相邻,求能 ...
- 337. House Robber III(包含I和II)
198. House Robber You are a professional robber planning to rob houses along a street. Each house ha ...
- Leetcode 337. House Robber III
337. House Robber III Total Accepted: 18475 Total Submissions: 47725 Difficulty: Medium The thief ha ...
- [LeetCode] 337. House Robber III 打家劫舍之三
The thief has found himself a new place for his thievery again. There is only one entrance to this a ...
- Leetcode之动态规划(DP)专题-198. 打家劫舍(House Robber)
Leetcode之动态规划(DP)专题-198. 打家劫舍(House Robber) 你是一个专业的小偷,计划偷窃沿街的房屋.每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互 ...
- php取默认值以及类的继承
(1)对于php的默认值的使用和C++有点类似,都是在函数的输入中填写默认值,以下是php方法中对于默认值的应用: <?phpfunction makecoffee($types = array ...
- IOS中取乱序数据最大值、最小值方法
2016-01-12 / 23:15:58 第一种方法也是常规方法,就是设定一个默认值作为最大值,循环取比这个最大值还大的值并赋值给默认最大值,这样循环完成后这个默认最大值变量里面的值就是最大值了: ...
- 在android的spinner中,实现取VALUE值和TEXT值。 ZT
在android的spinner中,实现取VALUE值和TEXT值. 为了实现在android的 spinner实现取VALUE值和TEXT值,我尝试过好些办法,在网上查的资料,都是说修改适配器, ...
随机推荐
- Examples For PLSQL Cursors - Explicit, Implicit And Ref Cursors
A cursor acts logically as a pointer into a result set. You can move the cursor through the result s ...
- VBA中自定义类和事件的(伪)注册
想了解一下VBA中自定义类和事件,以及注册事件处理程序的方法. 折腾了大半天,觉得这样的方式实在称不上“注册”,所以加一个“伪”字.纯粹是瞎试,原理也还没有摸透.先留着,有时间再接着摸. 做以下尝试: ...
- Oracle同义词学习
oracle的同义词总结 从字面上理解就是别名的意思,和视图的功能类似.就是一种映射关系. 同义词拥有如下好处: 节省大量的数据库空间,对不同用户的操作同一张表没有多少差别; 扩展的数 ...
- javascript练习----复选框全选,全不选,反选
第一种方式: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...
- 用Java集合中的Collections.sort方法对list排序的两种方法
用Collections.sort方法对list排序有两种方法第一种是list中的对象实现Comparable接口,如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...
- Win7_SendTo文件夹
Win7系统浮动sendto目录现在被移到了这里 : %APPDATA%\Microsoft\Windows\SendTo %APPDATA%是个环境变量,具体来说是在这里: C:\users\??? ...
- htm Dom对象与 Xml Dom对象的理解
html 是基于Xml的文档规范.是一种特殊的xml文档,这一点很重要 1.xml 文档的操作,java,c#,...各种语言都提供了很好的api对文档进行解析,操作.当然js 也不例外,提供了一系列 ...
- Java中JTree的简单案例
package ch12; import javax.swing.*; import javax.swing.tree.DefaultMutableTreeNode; /** * Created by ...
- linux下,一些关于动态库的问题:
程序运行是加载动态库的几种方法: 第一种,通过ldconfig命令 ldconfig是一个动态链接库管理命令,为了让动态链接库为系统所共享,还需运行动态链接库的管理命令它,ldconfig命令通 ...
- double 逆序
请设计一个函数,不许用到字符串函数,用数学运算,将double类型数据转换,例如123.456转换成654.321 int _tmain(int argc, _TCHAR* argv[]) { con ...