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.

Example

Given [3, 8, 4], return 8.

分析:

因为“偷”第一个或者“偷”第二个对后面的选择是有影响的,所以从后往前推算更好。

total[i] = Math.max(A[i] + total[i + 2], total[i + 1]);

A[i] + total[i + 2] 指的是偷第i家。

total[i + 1] 指的是不偷i家。

 public class Solution {
/**
* @param A: An array of non-negative integers.
* return: The maximum amount of money you can rob tonight
*/
public long houseRobber(int[] A) {
if (A == null || A.length == ) return ;
if (A.length == ) return A[];
if (A.length == ) return Math.max(A[], A[]); long[] total = new long[A.length];
int length = total.length;
total[length - ] = A[length - ];
total[length - ] = Math.max(A[length - ], A[length - ]); for (int i = length - ; i >= ; i--) {
total[i] = Math.max(total[i + ] + A[i], total[i + ]);
}
return total[];
}
}

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.

Example

nums = [3,6,4], return 6.

分析:

现在是一个环了,感觉好像找不到起始点。其实反过来想,就是头尾不能同时选而已。所以我们分别选取两个不同的起始点和结束点,跑两次就可以了。

 public class Solution {

     public int houseRobber2(int[] nums) {
if (nums == null || nums.length == ) return ;
if (nums.length == ) return nums[];
return Math.max(getMax(nums, , nums.length - ), getMax(nums, , nums.length - ));
} public int getMax(int[] nums, int start, int end) {
if (start == end) return nums[start];
if (start + == end) return Math.max(nums[start], nums[end]); int[] total = new int[end - start + ];
int m = total.length;
total[m - ] = nums[end];
total[m - ] = Math.max(nums[end], nums[end - ]); for (int i = m - ; i >= ; i--) {
total[i] = Math.max(nums[end - (m - i) + ] + total[i + ], total[i + ]);
}
return total[];
}
}

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

  3
/ \
2 3
\ \
3 1

Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.

    3
/ \
4 5
/ \ \
1 3 1

Maximum amount of money the thief can rob = 4 + 5 = 9.

分析:

看到树,想都不用多想,立马想到递归。关键是这个递归怎么写啊?

既然我们不知道是否把root包含进去是否是最优,那么我们就创建一个函数,返回一个数组,这个数组包含两种情况下的最大值。

 public class Solution {

     public int rob(TreeNode root) {
if(root == null) return ; int[] result = helper(root);
return Math.max(result[], result[]);
} public int[] helper(TreeNode root){
if(root == null){
return new int[]{, };
} int[] result = new int[];
int[] left = helper(root.left);
int[] right = helper(root.right); // result[0] is when root is selected, result[1] is when not.
result[] = root.val + left[] + right[];
result[] = Math.max(left[], left[]) + Math.max(right[], right[]); return result;
}
}

House Robber I & II & III的更多相关文章

  1. 解题思路:house robber i && ii && iii

    这系列题的背景:有个小偷要偷钱,每个屋内都有一定数额的钱,小偷要发家致富在北京买房的话势必要把所有屋子的钱都偷了,但是屋子之内装了警报器,在一定条件下会触发朝阳群众的电话,所以小偷必须聪明一点,才能保 ...

  2. Leetcode 137. Single Number I/II/III

    Given an array of integers, every element appears twice except for one. Find that single one. 本题利用XO ...

  3. 买卖股票的最佳时机I II III IV

    I 假设有一个数组,它的第i个元素是一支给定的股票在第i天的价格.如果你最多只允许完成一次交易(例如,一次买卖股票),设计一个算法来找出最大利润. II 假设有一个数组,它的第i个元素是一个给定的股票 ...

  4. LeetCode Single Number I / II / III

    [1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...

  5. 1. Two Sum I & II & III

    1. Given an array of integers, return indices of the two numbers such that they add up to a specific ...

  6. 【LeetCode】Single Number I & II & III

    Single Number I : Given an array of integers, every element appears twice except for one. Find that ...

  7. [Leetcode][JAVA] Best Time to Buy and Sell Stock I, II, III

    Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a gi ...

  8. LeetCode:Best Time to Buy and Sell Stock I II III

    LeetCode:Best Time to Buy and Sell Stock Say you have an array for which the ith element is the pric ...

  9. [Locked] Shortest Word Distance I & II & III

    Shortest Word Distance Given a list of words and two words word1 and word2, return the shortest dist ...

随机推荐

  1. 《Linux内核设计与实现》学习记录一

    chapter1 Linux内核简介 前言:Unix是一个具有相似应用程序编程接口(API)并且基于相似设计理念的操作系统家族. 1.1 Unix的历史 1.Unix演化版实现了任务管理.换页机制.T ...

  2. PHP开发APP接口实现--基本篇

    最近一段时间一直在做APP接口,总结一下APP接口开发以来的心得,与大家分享: 1. 客户端/服务器接口请求流程: 安卓/IOS客户端   –> PHP接口 –> 服务器端  –> ...

  3. OneZero第四周第四次站立会议(2016.4.14)

    1. 时间: 15:00--15:10  共计10分钟. 2. 成员: X 夏一鸣 * 组长 (博客:http://www.cnblogs.com/xiaym896/), G 郭又铭 (博客:http ...

  4. java自定义注解学习(二)_注解详解

    上篇文章,我们简单的实现了一个自定义注解,相信大家对自定义注解有了个简单的认识,这篇,这样介绍下注解中的元注解和内置注解 整体图示 内置注解 @Override 重写覆盖 这个注解大家应该经常用到,主 ...

  5. SVN for Mac

    SVN for Mac https://www.wikihow.com/Install-Subversion-on-Mac-OS-X https://subversion.apache.org/pac ...

  6. EntityFramework中Json序列化的循环引用问题解决--Newtonsoft.Json

    1.在使用EF时,由于数据库主外键关联,将对象进行Json序列化时会遇到循环引用的问题 //EF 中由于数据库主外键关联,对象的序列化经常出现循环引用问题 //使用.Net 自带的序列化工具,序列化出 ...

  7. nestd事务如果报错了 则回滚到外部事物保存点 且外部事物如果没异常的话 会正常提交 nested事务并不会提交;如果外部事物报错了 内部事务会一同回滚

    nestd事务如果报错了 则回滚到外部事物保存点 且外部事物如果没异常的话 会正常提交 nested事务并不会提交:如果外部事物报错了 内部事务会一同回滚

  8. Grafana elasticsearch 应用

    早期的时候,项目基于ES+echart写了一些仪表盘的展示页面,虽然ES配合这种char界面有着天然的优势,但实际写起代码来,还是很多重复的劳动,在一次偶然中发现Grafana,看到它提供了很多仪表盘 ...

  9. BZOJ2431 HAOI2009逆序对数列(动态规划)

    对于排列计数问题一般把数按一个特定的顺序加入排列.这个题做法比较显然,考虑将数从小到大加入排列即可. #include<iostream> #include<cstdio> # ...

  10. 【暴力Treap 或 离线归并】子串计数(genies)

    子串计数(genies) Description 给出一段含有n个元素的序列a,要求求出子串和小于等于t的子串个数 Input Data 输入共两行第一行包含两个整数,n,t分别表示序列a元素的个数和 ...