LeetCode House Robber II

【LeetCode】House Robber I & II 解题报告 【题目】 I 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 a 阅读详情

原题链接在这里:https://leetcode.com/problems/house-robber-ii/

是House Robber的进阶题。这里第一家和最后一家不能同时偷,所以像House Robber建立一个res1, 把从第一家到倒数第二家能偷最大值依次存入res1, 再建立一个res2, 把第二家到最后一家能偷最大值依次存入res2. 返回res1最后一个值 和res2最后一个值中较大者。

Note: 1. 不要忘记initialize res1[0],res1[1].

2. res2赋值时,用的是nums[i+1] , res1赋值时,用的是nums[i].

AC Java:

public class Solution {
    public int rob(int[] nums) {
        if(nums == null || nums.length == 0){
            return 0;
        }
        if(nums.length == 1){
            return nums[0];
        }
        if(nums.length == 2){
            return Math.max(nums[0],nums[1]);
        }
        
        int[] res1 = new int[nums.length-1];
        res1[0] = nums[0];
        res1[1] = Math.max(nums[0],nums[1]);
        for(int i = 2; i < res1.length; i++){
            res1[i] = Math.max(res1[i-2]+nums[i], res1[i-1]);
        }
        
        int[] res2 = new int[nums.length-1];
        res2[0] = nums[1];
        res2[1] = Math.max(nums[1],nums[2]);
        for(int i = 2; i < res2.length; i++){
            res2[i] = Math.max(res2[i-2]+nums[i+1],res2[i-1]); 
        }
        return Math.max(res1[nums.length-2], res2[nums.length-2]);
    }
}


House Robber(打家劫舍问题) 待补充 阅读详情

相关推荐

四自由度机械臂Matlab仿真全流程:从DH参数建模到轨迹规划实战

本文详细介绍了四自由度机械臂在Matlab中的完整仿真流程,从DH参数建模到轨迹规划实战。通过Robotics Toolbox工具,读者可以学习如何构建机械臂模型、验证运动学特性,并实现平滑的轨迹规划。文章还提供了性能优化技巧和常见问题解决方案,帮助开发者高效完成机械臂仿真项目。

weixin_29061041的博客 238

JAVA算法:House Robber I&II&III JAVA版本解题

JAVA算法:House Robber I&II&III JAVA版本解题 LeetCode198.House Robberhttps://leetcode.com/problems/house-robber/ LeetCode213.House Robber IIhttps://leetcode.com/problems/house-robber-ii/ Leet...

梅森上校的博客 业精于勤荒于嬉,形成于思毁于随。 414

Cadence17.4 Allegro PCB制板(gerber / 光绘)文件输出的顺序及注意点

Cadence Allegro PCB制板文件输出 (gerber 光绘文件输出)

weixin_45331044的博客 1万+

[LeetCode] 213. House Robber II

原题链接: https://leetcode.com/problems/house-robber-ii/ House Robber 专题相关题目 198. House Robber 1. 题目介绍 You are a professional robber planning to rob houses along a street. Each house has a certain amount ...

Ber03的博客 307

高效利用ChEMBL数据库:从靶点查询到活性数据处理的完整指南

本文提供了高效利用ChEMBL数据库的完整指南,从靶点查询到活性数据处理。详细介绍了靶点精准定位、高级过滤技巧、数据导出优化以及活性数据清洗、特征工程和可视化等实战技巧,帮助药物研发人员快速获取和处理生物活性数据,提升工作效率。

weixin_29278805的博客 791

Android应用之【巧妙隐藏桌面图标的进阶方案】

本文详细探讨了Android应用中隐藏桌面图标的进阶方案,解决了传统方法在Android 10及以上版本中出现的黑色图标残留问题。通过Intent-Filter配置和厂商适配技巧,实现子应用真正隐身,同时确保主应用正常启动。适用于企业工具套件、模块化系统等场景,提升用户体验和安全性。

weixin_28363123的博客 504

[leetcode]动态规划之House Robber II

动态规划之House Robber II题目思考代码 题目 leetcode入口 思考 刚开始想的dp[i]的解释是以nums[i]为结尾(包括nums[i])的最大数量,然后找不到状态方程。(这道题要和最长的子序列和区别开来,最长子序列和是连续的,故需要以dp[i]为结尾,而这道题可以是跳跃的,故不需要以dp[i]为结尾) 而把dp[i]理解为到位置i位置所能偷的最大数量,就能很容易的写出状态方...

crabstew的博客 276

实践指南——【SPSS操作解析】线性混合效应模型在重复测量数据中的应用

本文详细解析了线性混合效应模型在重复测量数据分析中的应用,并提供了SPSS操作指南。文章阐述了该模型如何通过区分固定效应与随机效应,有效处理数据非独立性和缺失值问题,从而获得更可靠的统计推断。

oo7890的博客 550

Leetcode 213. House Robber II(python+cpp)

Leetcode 213. House Robber II题目解法:动态规划 题目 You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are arranged i...

qq_37821701的博客 402

【华为机试真题 Python实现】分苹果

《华为机试真题》专栏含牛客网华为专栏、华为面经试题、华为OD机试真题。如果您在准备华为的面试,期间有想了解的可以私信我,我会尽可能帮您解答,也可以给您一些建议!本文解法非最优解(即非性能最优)。A,B两个人把苹果分为两堆,A希望按照它的计算规则等分苹果,他的计算规则是按照二进制加法计算,并且不计算走位,12+5=9(1100+0101=9), B的计算规则是十进制加法,包括正常进位,B希望在满足A的情况下获取苹果重量最多,输入苹果的数量和每个苹果重量,输出满足A的情况下获取的苹果总重量, 如果无法满足A的要

3412

极智AI | 谈谈为什么卷积加速更喜欢 NHWC Layout

本文主要讨论一下为什么卷积加速更加喜欢 NHWC 的数据排布。

极智视界 2970

leetcode 213 : House Robber II

leetcode 213 : House Robber II

西施豆腐渣 1万+

利用baostock获取上市公司财务指标

利用baostock获取上市公司的财务指标 1、导入模块,登录baostock import baostock as bs import pandas as pd import time # 登陆系统 lg = bs.login() # 显示登陆返回信息 print('login respond error_code:'+lg.error_code) print('login respond error_msg:'+lg.error_msg) 2、查询股票代码、行业板块 # 查询股票代码 rs=.

firefox_yau的博客 2098

LeetCode-213. House Robber II [C++][Java]

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place arearranged in a circle.That means the first house is the neighbor of the last one.

贫道绝缘子的博客 470

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

Leetcode 198. House Robber I II III

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

hello world 324

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

LeetCode //C - 213. House Robber II

【代码】LeetCode //C - 213. House Robber II。

Made in Code 1198

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

Leetcode House Robber II

Leetcode House Robber II 相关代码,本代码使用dp算法完成相关问题,本算法的关键为把存在环的退化成不带环的情况,在不带环的情况,用动态规化是相当容易的,以下是代码以及相关测试。算法复杂度为O(n)。#include <iostream> #include <vector> using namespace std;// the circumstance of circle c

Ttssxuan 587

[动态规划] leetcode 213 House Robber II

problem:https://leetcode.com/problems/house-robber-ii/ 多状态转换dp。我的方法是维护了四个状态。用两趟dp的基本思想也是多个状态。 class Solution { public: int rob(vector<int>& nums) { int n = n...

ayzejx8547408的博客 161

leetcode House Robber II

题目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, a

不积跬步,无以至千里 1746

LeetCode --- House Robber & House Robber II

House Roober 让我们分三种情况考虑: - 拿[1]号房子,那么[2]和[N]号不能再拿,另外[3...N-1]退化为了直线型 - 拿[N]号房子,那么[1]和[N-1]号不能再拿,另外[2...N-2]退化为了直线型 - [1]和[N]号房子都不拿,那么[2...N-1]退化为了直线型

DowntownGuy的博客 591

【Leetcode】213. 打家劫舍II(House Robber II)

Leetcode - 213 House Robber II (Medium) 题目描述:一个小偷沿着一条环形的街偷窃,给定数组表示每家屋子的金额,如果偷窃连续的两间屋子就会触发警报,求在不触发警报的前提下,偷窃的最大金额。 Input: [2,3,2] Output: 3 Explanation: You cannot rob house 1 (money = 2) and then rob h...

厉兵秣码 203

Java描述 LeetCode,213. House Robber II

大家好,我是河海哥,专注于后端,如果可以的话,想做一名code designer而不是普通的coder,一起见证河海哥的成长,您的评论与赞是我的最大动力。 1-1:题目 You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are arranged in a circle..

人在江湖 352

[LeetCode] House Robber II 打家劫舍之二

Note:This is an extension ofHouse 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 ...

weixin_34242819的博客 83

[LeetCode][JavaScript]House Robber II

House Robber II Note:This is an extension ofHouse 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 ...

weixin_30527551的博客 134

Leetcode -- House Robber II

https://leetcode.com/problems/house-robber-ii/description/前情回顾:http://blog.youkuaiyun.com/chaochen1407/article/details/44802541回顾完毕。好,我们知道之前那一题是一道非常简单典型的dp题。而这一题多加的条件就是把这条街变成circle,地球是圆的我们都知道,现在街道也是圆的。所以这增加...

chaochen1407的专栏 157
上一篇: LeetCode House Robber
下一篇: LeetCode Triangle
Dylan_Java_NYC
博客等级 码龄11年 1粉丝 134原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值