[leetcode]House Robber1,2
/**
* 一、
* 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.
二、
Note: This is an extension of House Robber. 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.
*/
/*
* 动态规划的一般套路,创建数组记录到当前这家时可能得到的最大收入,有两种情况,偷这家:res[i-2] + nums[i],不偷这家:res[i-1]
* 状态方程:两种情况取较大的
* 第二题可以分两种情况考虑,一种是偷第一家,则最后一家不偷,第二种就是偷最后一家,第一家不偷,两种情况的较大者就是结果*/
public class Q198HouseRobber {
public int rob(int[] nums) {
if (nums.length == 0 )
return 0;
int[] res = new int[nums.length+1];
res[0] = 0;
res[1] = nums[0];
for (int i = 2; i < nums.length+1; i++) {
res[i] = Math.max((res[i-2]+nums[i-1]),res[i-1]);
}
return res[nums.length];
}
public int rob2(int[] nums){
if (nums.length == 0 )
return 0;
if (nums.length == 1)
return nums[0];
int[] res1 = new int[nums.length];
int[] res2 = new int[nums.length+1];
//包含第一家的情况,最后一家肯定没有,所以循环的次数减1
res1[0] = 0;
res1[1] = nums[0];
for (int i = 2; i < nums.length; i++) {
res1[i] = Math.max((res1[i-2]+nums[i-1]),res1[i-1]);
}
//不包含第一家的情况,
res2[0] = 0;
res2[1] = 0;
for (int i = 2; i < nums.length+1; i++) {
res2[i] = Math.max((res2[i-2]+nums[i-1]),res2[i-1]);
}
return Math.max(res1[res1.length-1],res2[res2.length-1]);
}
}
[leetcode]House Robber1,2的更多相关文章
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
随机推荐
- 使用@RequestBody注解获取Ajax提交的json数据
最近在学习有关springMVC的知识,今天学习如何使用@RequestBody注解来获取Ajax提交的json数据内容. Ajax部分代码如下: 1 $(function(){ 2 $(" ...
- 16.java设计模式之迭代器模式
基本需求: 展示一个学校的结构,比如一个学校下面有多个学院,学院下面有多个系,对其节点主要是遍历,与组合模式略有不同 传统方案: 学校<-学院<-系 依次继承 这种方式,在一个页面中展示出 ...
- Pycharm永久激活方法
1.下载新版破解补丁 链接 https://pan.baidu.com/s/137-afPKYfkXbvroSv1hoYw 提取码: cm43 下载补丁文件jetbrains-agent.jar并将 ...
- OpenCV阈值处理函数threshold处理32位彩色图像的案例
☞ ░ 前往老猿Python博文目录 ░ 一.概述 openCV图像的阈值处理又称为二值化,之所以称为二值化,是它可以将一幅图转换为感兴趣的部分(前景)和不感兴趣的部分(背景).转换时,通常将某个值( ...
- PyQt(Python+Qt)学习随笔:QTreeWidget树型部件中的QTreeWidgetItem项构造方法
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 QTreeWidget树型部件的项是单独的类对象,这个类就是QTreeWidgetItem. QTr ...
- 第15.26节 PyQt(Python+Qt)入门学习:Model/View架构中的便利类QListWidget详解
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 一.概述 列表部件(List Widget)对应类QListWidget,是从QListView派生 ...
- PyQt学习随笔:Model/View开发时从Model相关类派生自定义类需要注意的问题
在<PyQt学习随笔:重写setData方法截获Model/View中视图数据项编辑的注意事项>介绍的方法,从Model相关类派生自定义类,通过重写setData方法以获取View中数据的 ...
- JMeter断言/检查点
断言就类似LoadRunner中的检查点.对上一个请求返回的信息,获取部分字符串.图片等做判断,确保返回的信息的准确性. 右键点击"HTTP请求" -> "添加&q ...
- SQL Server常用函数及命令
1.字符串函数 --ascii函数,返回字符串最左侧字符的ascii码值 SELECT ASCII('a') AS asciistr --ascii代码转换函数,返回指定ascii值对应的字符 SEL ...
- 从输入 URL 到页面展示,这中间发生了什么?
当面试官问到,请你说说看"从输入 URL 到页面展示,这中间发生了什么?" 以前的我是这样回答的: 用户输入URL后,向服务器端发起请求.如果顺利,得到网络响应之后,浏览器对资源进 ...