[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 ...
随机推荐
- Jmeter-BeanShell断言的运用二(不同Json格式的字段提取和断言判断)
前言 为了更加熟悉BeanShell,所以用几个实例来记录说明下,不同的Json格式是怎么提取相应字段和判断断言的.(会持续更新...) 一.第一种Json格式 1.Json响应数据内容如下: { & ...
- 基于java实现的简单网页日历功能,有兴趣得可以把它转换到前端实现
之前做项目的时候,因为要用到不同日期显示不同的内容,就自己做了一个日期的显示和选择功能,今天抽空把以前的代码理了一下,顺便就把之前做的日期功能给拿出来回顾一下,大家可以提点意见,帮忙完善下设计.先上一 ...
- 记STM32F103C8T6+STLINK下载器在Keil中的设置
调试代码为: /************************************** * 文件名 :main.c * 描述 :获取CPU的96bit ID 和 flash的大小,并通过USAR ...
- lcm的和(莫比乌斯反演)
马上开学了,加一个操作系统和数据库标签 不玩了,求1-n和1-m的lcm(i,j)和 首先想到把lcm(i,j)转化为i * j / gcd(i, j) 然后gcd,要素察觉,开始枚举d使得gcd(i ...
- 第15.42节、PyQt输入部件:QFontComboBox、QLineEdit、QTextEdit、QPlainText功能详解
专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 一.引言 输入部件量比较多,且功能很丰富,但除了用于编写编辑器.浏览器 ...
- PyQt(Python+Qt)学习随笔:Qt Designer中怎么给toolBar添加按钮
在Designer中创建了一个MainWindow窗体,当想在其中的toolBar中添加toolButton时发现怎么也放不上去,最终才发现toolBar中的按钮只能通过直接拖拽Action编辑器中的 ...
- Hbase的基本原理(与HIVE的区别、数据结构模型、拓扑结构、水平分区原理、场景)
重点:HBase的基本数据模型.拓扑结构.部署配置方法,并介绍通过命令行和编程方式使用HBase的基本方法. HBase:一种列存储模式与键值对相结合的NoSQL软件,但更多的是使用列存储模式,底层的 ...
- 安装nginx并安全地配置和启动
一.安装nginx >>参考文章<< 安装教程,看代码&注释 # .sh # 如果centos服务器是最低安装,则先安装weget yum install -y wge ...
- 团队项目6——Alpha阶段项目复审
复审团队 广东靓仔六强选手 复审员 钟俊豪(3118005122) 复审内容 小组名称和链接 优点 缺点&Bug报告 最终排名 代码敲不队https://www.cnblogs.com/pip ...
- 戴尔iDRAC+Ubuntu 18.04系统安装
Ubuntu镜像下载链接:http://mirrors.aliyun.com/ubuntu-releases/18.04/ 1.登录戴尔管理口 2.点击虚拟控制台 3.选择镜像 4.挂载镜像 5.选择 ...