/**
* 一、
* 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的更多相关文章

  1. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  2. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  3. [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 ...

  4. 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 ...

  5. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  6. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  7. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  8. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  9. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

随机推荐

  1. python办公入门7:xlwt

    xlwt写入excel步骤 创建工作簿 创建工作表 填充工作表内容 保存文件 1 import xlwt 2 3 #创建工作簿 4 wb=xlwt.Workbook() 5 #创建工作表 6 ws=w ...

  2. linux系统下oracle表空间占用情况

    1.我们先查询表空间的占用情况,使用sql如下: select upper(f.tablespace_name) "表空间名", d.tot_grootte_mb "表空 ...

  3. 冲刺随笔——Day_Six

    这个作业属于哪个课程 软件工程 (福州大学至诚学院 - 计算机工程系) 这个作业要求在哪里 团队作业第五次--Alpha冲刺 这个作业的目标 团队进行Alpha冲刺 作业正文 正文 其他参考文献 无 ...

  4. flask:蓝图--blueprint

    一.蓝图 1.什么是蓝图?(WHAT) 1)蓝图就是模块化处理的类 2)用于实现单个应用的视图.模板.静态文件的集合 总结:蓝图就是一个存储操作路由映射方法的容器,主要用来实现客户端请求和URL相互关 ...

  5. 无需付费,教你IDEA社区版中开发Web项目(SpringBoot\Tomcat)

    1.IDEA 版本介绍 最近有小伙伴私信我说 IDEA 破解怎么总是失效?难道就没有使用长一点的吗... 咳咳,除了给我留言「激活码」外,或许社区版可能完全满足你的需求. 相信有挺多小伙伴可能不清楚或 ...

  6. flask注册蓝图报错

    记录下这个我找了两天的坑... take no arguments() 这两天一直学习flask的时候,我把注册的蓝图,写成注册的form表单的 举个栗子 class TetsView(view.Me ...

  7. 什么时候使用transition?什么时候使用animation?

    不同点: 1. 触发条件不同.transition通常和hover等事件配合使用,由事件触发.animation则和gif动态图差不多,立即播放. 2. 循环. animation可以设定循环次数. ...

  8. 题解-CF429C Guess the Tree

    题面 CF429C Guess the Tree 给一个长度为 \(n\) 的数组 \(a_i\),问是否有一棵树,每个节点要么是叶子要么至少有两个儿子,而且 \(i\) 号点的子树大小是 \(a_i ...

  9. 最简 Spring IOC 容器源码分析

    前言 BeanDefinition BeanFactory 简介 Web 容器启动过程 bean 的加载 FactoryBean 循环依赖 bean 生命周期 公众号 前言 许多文章都是分析的 xml ...

  10. shell--检查apache是否启动脚本

    #首先我们需要检查apache是否以启动,这里我们用到的说nmap命令,Linux默认情况下是没有安装nmap命令的. #那么我们需要安装下nmap,安装的命令很简单:yum -y install n ...