198.HouseRobber

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.

Tips:你是一名职业抢劫犯,计划在街上抢劫房屋。每个房子都有一定数量的钱,唯一能阻止你抢劫的限制就是相邻的房子有相连的安全系统,当相邻的两个房子在同一晚被抢劫,就会自动报警。给定一组非负整数,表示每个房子的钱数,确定你今晚可以抢劫的最大金额,而不用报警。

输入:int[] 表示每个房子内的钱数。

输出:能抢劫的最大钱数。

只要比较当前值,与前一个值加上nums[]之和的大小即可。

package easy;

public class L198HouseRobber {

    public int rob(int[] nums) {
int pre = 0;
int cur=0;
int len = nums.length;
if (nums == null || len == 0)
return 0;
for(int i=0;i<len;i++){
int temp=Math.max(pre+nums[i], cur);
pre=cur;
cur=temp;
}
return cur;
} public static void main(String[] args) {
int[] nums = { 2, 1, 1, 2 ,1,1};
int[] nums1 = { 1, 2, 3, 4, 5, 6, 7, 8 };
int[] nums2={2,7,9,3,1};
L198HouseRobber l98 = new L198HouseRobber();
int money = l98.rob(nums2);
System.out.println(money);
}
}

动态规划的方法,维护一个数组dp,dp[i]表示到i位置时不相邻数能形成的最大和。

public int rob(int[] nums) {
if(nums.length<=0) return 0;
if(nums.length==1) return nums[0];
int[] dp=new int[nums.length];
dp[0]=nums[0];
dp[1]=Math.max(nums[0],nums[1]);
for(int i=2;i<nums.length;i++){
dp[i]=Math.max(dp[i-1],dp[i-2]+nums[i]);
}
return dp[nums.length-1];
}

213. House Robber II

Question: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.

Tips:小偷换了一个地方接着偷,这次所有的房子围成一个圈。怎么偷钱最多,而不惊动警察。

思路:由于上一个题目房子在一条直线上,现在的房子是在一个圆环内,我们可以想办法将圆环拆成直线。第一步首先决定是否偷第一个房子。可以将问题拆分为两种情况,(0~nums.length-1)与(1~nums.length-2).拆分之后就又变会第一个问题。

class Solution {
public int rob(int[] nums) {
int len=nums.length;
if(len<=0) return 0;
if(len==1) return nums[0];
return Math.max(rob(nums,0,len-2),rob(nums,1,len-1));
}
public int rob(int[] nums,int start,int end) {
int[] dp = new int[nums.length]; if(start==0){
dp[0]=nums[0];
dp[1]=Math.max(nums[0],nums[1]);
}
else{
dp[1]=nums[1];
}
for(int i=2;i<=end;i++){
dp[i]=Math.max(dp[i-1],dp[i-2]+nums[i]);
}
return dp[end];
}
}

337. House Robber III

Question: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.

Tips:小偷又换一个地方接着偷。此地房子只有一个入口叫root,每个房子有且只有一个父房子。求可偷最大金额。

思路:这个题给我的第一感觉就是需要按层得出财富最大值,分为两种情况:

(1)包含root结点,以及之后的第三层结点

(2)不包含root结点,从第二层开始隔层相加。

代码:

  public int rob(TreeNode root) {
if(root==null)return 0;
int ans=0;
if(root.left!=null){
ans+=rob(root.left.left)+rob(root.left.right);
}
if(root.right!=null){
ans+=rob(root.right.left)+rob(root.right.right);
}
return Math.max(ans+root.val,rob(root.left)+rob(root.right));
}

但是这种方法存在重复计算非常多。所以我们可以通过保存计算结果来减少计算量。(使用hashmap存储中间结果。)

 public int rob(TreeNode root){
if(root==null)return 0;
HashMap<TreeNode,Integer> map= new HashMap<>();
return rob(root,map);
}
public int rob(TreeNode root,HashMap map) {
if(root==null)return 0;
int ans=0;
if(map.containsKey(root)){
return (int)map.get(root);
}
if(root.left!=null){
ans+=rob(root.left.left,map)+rob(root.left.right,map);
}
if(root.right!=null){
ans+=rob(root.right.left,map)+rob(root.right.right,map);
}
ans=Math.max(ans+root.val,rob(root.left,map)+rob(root.right,map));
map.put(root,ans);
return ans;
}

【leetcode】198.HouseRobber的更多相关文章

  1. 【LeetCode】198. House Robber 打家劫舍 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 递归 + 记忆化 动态规划 优化动态规划空间 ...

  2. 【LeetCode】198 - House Robber

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...

  3. 【LeetCode】198. 打家劫舍

    打家劫舍 你是一个专业的小偷,计划偷窃沿街的房屋.每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警. 给定 ...

  4. 【LeetCode】386. Lexicographical Numbers 解题报告(Python)

    [LeetCode]386. Lexicographical Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  5. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  6. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  7. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  8. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  9. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

随机推荐

  1. java虚拟机内存管理

    1. java虚拟机内存如下 2. 运行时数据区 内存图分析:

  2. kubernetes基本概念和术语

    一. kubernetes是什么 kubernetes是一个全新得基于容器技术得分布式架构领先方案.是google Borg得一个开源版本.Borg是google得一个久负盛名得内部使用得大规模集群管 ...

  3. 20155331 2016-2017-2 《Java程序设计》

    20155331 2016-2017-2 <Java程序设计> 教材学习内容总结 理解封装,继承和多态. 封装最简单的理解就是包装,把编译的class文件封装起来,便于管理,还可以设置密码 ...

  4. 20155336 实验三 敏捷开发与XP实践

    20155336 实验三 敏捷开发与XP实践 实验内容 XP基础 XP核心实践 相关工具 实验内容及步骤 (一)编码标准:在IDEA中使用工具(Code->Reformate Code)把代码重 ...

  5. 20145226夏艺华 EXP5 MSF基础应用

    实践目标 · 掌握metasploit的基本应用方式. · 具体需要完成 (1)ms08_067; (2)ms11_050: (3)Adobe (4)成功应用任何一个辅助模块. 报告 本次实验一共用到 ...

  6. C#基础之委托

    委托常常和事件在一起使用,可以理解委托是方法的容器,事件则是委托的另一种表现形式.委托和事件虽然写得还比较多,不过也只是用的熟练而已,趁周末没课好好巩固下基础,一点一点积累吧. 1.一个简单的小例子 ...

  7. dsp6657的helloworld例程测试-第二篇-CFG文件

    1. 上一篇疑问,int StackTest()这个函数是怎么运行的,后来在.cfg文件找到了答案,.cfg包含丰富的信息,对于用惯C语言的,确实不太习惯 var Memory = xdc.useMo ...

  8. Cannot get connection for URL jdbc:oracle:thin:调用中无效参数

    这个报错明显是连接数据库的url没有写对,但是,我要说的是但是,同样的代码生产没有问题,而测试环境报错了.最终哥找到那个错误,jdbc连接数据库时,有ResultSet,PreparedStateme ...

  9. nginx基础配置加基础实战演示

    目录 基本配置 设置用户 工作衍生进程数 错误日志存放路径 pid文件存放路径 设置最大连接数 http->server gzip 字符编码 nginx的基本格式 实战配置 虚拟主机配置 开始配 ...

  10. Selenium自动化测试第二天(上)

    如有任何学习问题,可以添加作者微信:lockingfree 目录 Selenium自动化测试基础 Selenium自动化测试第一天(上) Selenium自动化测试第一天(下) Selenium自动化 ...