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 -- 不取相邻值的最大值的更多相关文章

  1. (leetcode:选择不相邻元素,求和最大问题):打家劫舍(DP:198/213/337)

    题型:从数组中选择不相邻元素,求和最大 (1)对于数组中的每个元素,都存在两种可能性:(1)选择(2)不选择,所以对于这类问题,暴力方法(递归思路)的时间复杂度为:O(2^n): (2)递归思路中往往 ...

  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:二叉树下的不能相邻,求能 ...

  3. 337. House Robber III(包含I和II)

    198. House Robber You are a professional robber planning to rob houses along a street. Each house ha ...

  4. Leetcode 337. House Robber III

    337. House Robber III Total Accepted: 18475 Total Submissions: 47725 Difficulty: Medium The thief ha ...

  5. [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 ...

  6. Leetcode之动态规划(DP)专题-198. 打家劫舍(House Robber)

    Leetcode之动态规划(DP)专题-198. 打家劫舍(House Robber) 你是一个专业的小偷,计划偷窃沿街的房屋.每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互 ...

  7. php取默认值以及类的继承

    (1)对于php的默认值的使用和C++有点类似,都是在函数的输入中填写默认值,以下是php方法中对于默认值的应用: <?phpfunction makecoffee($types = array ...

  8. IOS中取乱序数据最大值、最小值方法

    2016-01-12 / 23:15:58 第一种方法也是常规方法,就是设定一个默认值作为最大值,循环取比这个最大值还大的值并赋值给默认最大值,这样循环完成后这个默认最大值变量里面的值就是最大值了: ...

  9. 在android的spinner中,实现取VALUE值和TEXT值。 ZT

    在android的spinner中,实现取VALUE值和TEXT值.   为了实现在android的 spinner实现取VALUE值和TEXT值,我尝试过好些办法,在网上查的资料,都是说修改适配器, ...

随机推荐

  1. 读取视屏文件,保存帧图片为ppm文件

    ffmpeg跟sdl的学习过程:一.版本信息:ffmpeg-3.0.2.tar.bz2SDL2-2.0.4.tar.gz二.编译过程:1.ffmgeg的编译:./configure --enable- ...

  2. 在CentOS下安装Redis

    Redis比较傲娇,在windows上还没有很好的安装方式,不得已搞了个虚拟机玩玩. 装Redis十分简单,按照下面的几个命令来就行了. 安装命令 wget http://download.redis ...

  3. 别名alias

    alias #查看已设置的别名 alias  别名='原命令' #暂时设定别名(重启失效):alias ls='ls --color=never' unalias  别名 #删除别名 设置别名永久生效 ...

  4. poj 1556 (Dijkstra + Geometry 线段相交)

    链接:http://poj.org/problem?id=1556 The Doors Time Limit: 1000MS   Memory Limit: 10000K Total Submissi ...

  5. C语言中strdup函数使用方法

    头文件:#include <string.h> 定义函数:char * strdup(const char *s); 函数说明:strdup()会先用malloc()配置与参数s 字符串相 ...

  6. python_way day12 RabbitMQ ,pymysql

    python_way day12 1.RabbitMQ 2.pymysql RabbitMQ 1.基本用法 """ producer """ ...

  7. 个人博客作业WEEK 1

    一.项目时间规划与实际用时 PSP2.1 Personal Software Process Stages 预计时间/h 实际时间/h Planning 计划   · Estimate · 估计这个任 ...

  8. select实现输入模糊匹配与选择双重功能

    下载jqueryUI插件 引入 <link rel="stylesheet" type="text/css" href="/js/jquery/ ...

  9. Python中的__new__()方法的使用

    __new__() 函数只能用于从object继承的新式类. 先看下object类中对__new__()方法的定义: class object:   @staticmethod # known cas ...

  10. 转载: C++ 转换构造函数 和 类型转换函数

    1.对于系统的预定义基本类型数据,C++提供了两种类型转换方式:隐式类型转换和显式类型转换. ,sum; double b=5.55; sum=a+b;//-------(1) std::cout&l ...