/**
* 一、
* 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. Leetcode 021 Merge Two Sorted Lists

    摘要:Merge two sorted linked lists and return it as a new list. The new list should be made by splicin ...

  2. 去除openwrite.cn博客验证码限制

    相信有的小伙伴肯定遇到过如下这种情况,但是作为老白嫖党肯定是 「下次一定」 了,所以今天我们来看看如何不关注公众号实现 「阅读原文」. 如何解决呢? 1.通过 F12 打开控制台,切换至 Elemen ...

  3. Spring Cloud Alibaba 初体验(一) Nacos 配置中心

    一.Nacos 下载与初始化配置 本文使用1.2.0,下载地址:https://github.com/alibaba/nacos/releases Nacos 单机模式支持持久化配置到 MySQL 数 ...

  4. TextClip的list和search方法报错:UnicodeDecodeError: utf-8 codec canot decode byte 0xb7 in position 8

    ☞ ░ 前往老猿Python博文目录 ░ 由于moviepy对多语言环境支持存在一些问题,因此在执行TextClip.list('font')和TextClip.search('GB','font') ...

  5. PyQt学习随笔:QTableWidget的selectedRanges、setRangeSelected访问选中矩形范围的方法

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 在QTableWidget对项的操作支持选中多个项的情况下,可以通过方法selectedRanges ...

  6. LeetCode初级算法之数组:122 买卖股票的最佳时机 II

    买卖股票的最佳时机 II 题目地址:https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/ 给定一个数组,它的第 i ...

  7. 第 5 篇 Scrum 冲刺博客

    每天举行会议 会议照片: 昨天已完成的工作与今天计划完成的工作及工作中遇到的困难: 成员姓名 昨天完成工作 今天计划完成的工作 工作中遇到的困难 蔡双浩 实现重设计个人界面的功能添加 实现关注,被关注 ...

  8. C# 9.0新特性详解系列之五:记录(record)和with表达式

    1 背景与动机 传统面向对象编程的核心思想是一个对象有着唯一标识,表现为对象引用,封装着随时可变的属性状态,如果你改变了一个属性的状态,这个对象还是原来那个对象,就是对象引用没有因为状态的改变而改变, ...

  9. 「IOI2017」西默夫 的一个另类做法

    我们发现如果我们有一个环套树的话,那么我们可以把这个环套树去掉每一条环上的边\(e\),问一遍有多少御道在这棵树上.假设删去\(e\)后答案为\(A_e\). 如果答案全部一样,那么说明环上的边都不在 ...

  10. 「IOI2017」接线 的另类做法

    看到这题,我的第一反应是:这就是一个费用流模型?用模拟费用流的方法? 这应该是可以的,但是我忘记了怎么模拟费用流了IOI不可能考模拟费用流.于是我就想了另外一个方法. 首先我们考虑模拟费用流的模型如下 ...