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. Python学习:14.Python面向对象(一)

    一.面向对象简介 Python设计之初,就是一门面向对象的语言,在Python中一切皆对象,而且在Python中创建一个对象也很简单,今天我们就来学习一下Python的面向对象的知识. 二.两种编程方 ...

  2. upload-labs19记录

    upload-labs19记录 本次做题为白盒,因为只是fuzz的话并不能学到什么,所以从漏洞源下手. 前端校验 Pass1 源码 function checkFile() { var file = ...

  3. PICT工具一键生成正交试验用例

    PICT工具一键生成正交试验用例 作用: 1.解决手动设计大量测试用例.或覆盖不全面问题,提高测试效率 2.读取excel,将生成的参数组合自动带入脚本,进行接口自动化测试 一.PICT简介 PICT ...

  4. 浏览器内置http抓包工具软件

    使用 360极速浏览器抓取HTTP的 请求头 与 响应头: *** 从 工具--->开发人员工具(F12) 或者 直接按下 F12键,打开抓包工具: **** 按下F5刷新: 双击其中的 Nam ...

  5. Binary Indexed Tree (Fenwick Tree)

    Binary Indexed Tree 主要是为了存储数组前缀或或后缀和,以便计算任意一段的和.其优势在于可以常数时间处理更新(如果不需要更新直接用一个数组存储所有前缀/后缀和即可).空间复杂度O(n ...

  6. nodejs搭建web服务器初级

    nodejs搭建简单的web服务器 1.1简介 Node.js是基于Chrome JavaScript运行时建立的一个平台,实际上它是对Google Chrome V8引擎进行了封装,它主要用于创建快 ...

  7. 20155210 潘滢昊 2016-2017-2 《Java程序设计》第3周学习总结

    20155210 2016-2017-2 <Java程序设计>第3周学习总结 教材学习内容总结 class xxx{ }:定义类 运用: class Clothes{ String col ...

  8. 2017-2018-1 20155320加分项目——pwd的实现

    2017-2018-1 20155320加分项目--pwd的实现 1 学习pwd命令 2 研究pwd实现需要的系统调用(man -k; grep),写出伪代码 3 实现mypwd 4 测试mypwd ...

  9. C语言复习20170826

    数组 先定义,再初始化,最后使用. 访问数组中的元素可以采用数组名加下标的方式,下标是从0开始,c并不检查数组下标是否越界,所以在访问数组中的元素时需要注意,需要对数组下标做判断,防止访问数组越界. ...

  10. #2017-2018-1 20155327 《信息安全系统设计基础》实现mypwd

    2017-2018-1 20155327 <信息安全系统设计基础>实现mypwd Linux pwd命令用于显示工作目录. 执行pwd指令可立刻得知您目前所在的工作目录的绝对路径名称. p ...