[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 ...
随机推荐
- 面经手册 · 第20篇《Thread 线程,状态转换、方法使用、原理分析》
作者:小傅哥 博客:https://bugstack.cn Github:https://github.com/fuzhengwei/CodeGuide/wiki 沉淀.分享.成长,让自己和他人都能有 ...
- SSM框架之MyBatis框架实现简单的增删改查
MyBatis框架介绍 MyBatis是一个优秀的数据持久层框架,在实体类和SQL语句之间建立映射关系是一种半自动化的ORM实现,其封装性要低于Hibernate,性能优越,并且小巧,简单易学,应用也 ...
- CentOS创建管理LVM分区(挂载新分区)
来源: CentOS 8.0 创建管理LVM分区(挂载新分区) https://www.iwmyx.cn/centos80cjgllvmfa.html 1.查看可用磁盘 fdisk -l 2.新盘(/ ...
- 第8章 Python类中常用的特殊变量和方法目录
第8章 Python类中常用的特殊变量和方法 第8.1节 Python类的构造方法__init__深入剖析:语法释义 第8.2节 Python类的__init__方法深入剖析:构造方法案例详解 第8. ...
- 第14.15节 爬虫实战1:使用Python和selenium实现csdn博文点赞
写在前面:本文仅供参考学习,请勿用作它途,禁止转载! 在开始学爬虫时,学习了csdn博客专家(id:明天依旧可好 | 柯尊柏)<实战项目二:实现CSDN自动点赞>的文章,文章介绍了通过Py ...
- PyQt(Python+Qt)学习随笔:Qt Designer中部件的焦点策略focusPolicy设置
在Qt Designer中可以设置部件的焦点策略,部件的焦点策略属性取值范围由枚举类型Qt.FocusPolicy来定义,该枚举类型及其含义如下表所示: 注意:经老猿测试鼠标轮滚动获取焦点,只有在鼠标 ...
- SASRec 实践
SASRec--2018,ICDM,论文<Self-Attentive Sequential Recommendation> 源代码链接:https://github.com/kang20 ...
- new一个对象时,会经历哪些步骤
(1)创建一个对象:(2)将构造函数的作用域赋值给新对象(因此this就指向了这个新对象):(3)执行构造函数中的代码(为这个新对象添加属性):(4)返回新对象
- 手把手教你写DI_2_小白徒手撸构造函数注入
小白徒手撸构造函数注入 在上一节:手把手教你写DI_1_DI框架有什么? 我们已经知道我们要撸哪些东西了 那么我们开始动工吧,这里呢,我们找小白同学来表演下 小白同学 :我们先定义一下我们的广告招聘纸 ...
- nginx配置访问本地资源
参考博客:https://www.cnblogs.com/xy51/p/9973326.html 需要访问路径:http://IP:10013/p1upgrade/picfiles/image73b4 ...