LeetCode House Robber

【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 阅读详情

LeetCode House Robber

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.


题目大意是给你一个保存正数的数组,要你根据要求计算最大和(要求:每次不能选择相邻的两个数)

解题思路:DP问题。对于某个元素nums[i],是否并入和中主要有两点(sum1表示偶数下标的和,sum2表示奇数下标的和):
i为偶数:sum1 = max(sum1 + nums[i], sum2)
i为奇数:sum2 = max(sum2 + nums[i], sum1)

class Solution {
    #define max(a,b) (a>b?a:b)
public:
    int rob(vector<int>& nums) {
        int sum1 = 0, sum2 = 0;
    	int count = nums.size();
    	if (0 == count)
    	{
    		return 0;
    	}
    	
    	for (int i = 0; i < count; i++)
    	{
    		if (i % 2 == 0)
    			sum1 = max(sum1 + nums[i], sum2);
    		else
    			sum2 = max(sum2 + nums[i], sum1);
    	}
    
    	return max(sum1, sum2);
        }
};

Mac上VMware Fusion手动安装VMware Tools实现跨系统复制粘贴 本文详细指导了在Mac的VMware Fusion中手动安装VMware Tools的完整流程,以解决虚拟机与宿主机之间无法复制粘贴的常见问题。通过定位安装镜像、挂载、解压并运行安装脚本,最终实现跨系统无缝数据交换,提升工作效率。 阅读详情

相关推荐

【PLC】三菱PLC控件安装疑难解析:Error Number:-119的全面解决方案

本文针对三菱PLC控件安装过程中常见的Error Number:-119错误,提供了深度解析与实战解决方案。该错误通常源于Windows系统权限限制或文件冲突。文章从理解错误本质入手,逐步介绍了以管理员身份运行、手动注册DLL文件、采用新版环境搭配旧版控件的黄金组合以及系统环境修复等四大招法,帮助用户彻底解决这一安装难题,确保工控软件稳定运行。

jj890的博客 494

Leetcode: House Robber

最近做了leetcode上三道动态规划的题,觉得很不错,题目的评价也不错,故作此记录。 1、198. House Robber 题意:一组直线排列的房屋,一个小偷要进屋偷钱,如果小偷偷了相邻两座房屋就会触发报警系统,问在不触发报警系统的前提下小偷最多可以偷到多少钱。 难度:esay 思路: 1、由于是线性排列,所以一间房屋的相邻房屋只有其左右两间房屋,当我们依次查看房屋时,则一间房屋的相...

平凡 562

数电实训:万年历

万年历

Lmyccq的博客 1万+

【Leetcode】House Robber

题目链接:https://leetcode.com/problems/house-robber/ 题目: You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint st

业精于勤,荒于嬉 3315

解决若依Vue3前后端分离---路由切换时显示白屏

解决基于Vue3若依前后端分离项目中出现的路由正常切换但是就是不显示数据的问题,也就是不发起网络请求的问题。

简单点的博客 1604

3D Slicer的学习(二) 图像的分割

先对不同层级标注,然后点击Fill between slices -> Initialize -> Apply拖动层级颜色区域就会变成连续自适应的动态变化区域。如图,点击Level tracing,在图像上放置鼠标,会显示可能想要选中区域的轮廓,点击鼠标右键会将其显示为蓝色。然后点击Grow from seeds -> Initialize -> Apply,显示3D之后会自动扩展到其他位置的相同部位,从而展示整个部位。如图为copy之后,将黄色区域的颜色复制到原本的绿色区域。

tuweibazong的博客 6080

House Robber Leetcode 198,213,337

House Robber Leetcode 题目描述 你是一个专业的小偷,计划偷窃沿街的房屋。每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警。 给定一个代表每个房屋存放金额的非负整数数组,计算你在不触动警报装置的情况下,能够偷窃到的最高金额。 题目分析 动态规划。令 ans[i]ans[i]ans[i]...

qq_36835991的博客 419

实战解析:从Innovus到Calibre的LVS错误排查全流程

本文详细解析了从Innovus到Calibre的LVS错误排查全流程,涵盖物理验证关键步骤与工具链协同。通过实战案例分享,重点解决器件不匹配、连接性错误等常见问题,并提供GDS导出规范、网表修正策略等实用技巧,帮助工程师高效完成芯片设计物理验证。

9o8p7i6u5y的博客 1034

House Robber LeetCode动态规划

LeetCode上动态规划的一个系列,从多个角度来考虑问题,从而更加熟练动态规划,加深理解和精通应用。 House Robber I 解法1: 递归 解法2: 递归+memorization 解法3: 动态规划1: 两个动态规划数组dp[i][0]和dp[i][1]分别表示在第i个位置不抢和抢的收益。 dp[i][0] = max(dp[i-1][0], dp[i-1][1]) dp[i][1] ...

大音希声大象无形 211

从零构建MAVLink通信:PX4飞控与QGC的协议握手全解析

本文详细解析了PX4飞控与QGC地面站通过MAVLink协议进行通信的全过程,重点介绍了航点传输协议的状态机设计、INT类型消息的优化优势以及异常处理与调试技巧。通过实战案例和性能优化建议,帮助开发者高效构建无人机通信系统,提升数据传输效率与稳定性。

weixin_29209805的博客 504

用Python turtle库 绘制皮卡丘

Turtle库是Python语言中一个很流行的绘制图像的函数库,想象一个小乌龟,在一个横轴为x、纵轴为y的坐标系原点,(0,0)位置开始,它根据一组函数指令的控制,在这个平面坐标系中移动,从而在它爬行的路径上绘制了图形。 turtle基础知识参考: https://gitchat.blog.youkuaiyun.com/article/details/90696190 https://blog.csdn.ne...

qq_36295856的博客 2万+

【LeetCode】213. House Robber ||

213. House Robber II Description: 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 c...

Microstrong 1030

【DaVinci Developer专题】-23-IDT高阶应用:结构体类型的数组

共8页 目录 1 创建一个Record IDT 2 创建一个Array IDT 3 创建一个S/R Port Interface 4 创建一个Constant 5 导入CFG生成代码的总结 6 结尾 <--返回「实战篇」博文-总目录--> <--返回「Autosar从入门到精通-实战篇」专栏主页--> 关键字: 缩略语/术语 含义 DEV DaVinci Developer .

「汽车电子助手」的博客 1667

Leetcode House Robber系列

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

王旻宇 504

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

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

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

LeetCode:House Robber系列

198. House Robber 已经有说明了,这篇第九个题。 http://blog.youkuaiyun.com/bestzem/article/details/51884230 213. House Robber IINote: This is an extension of House Robber.After robbing those houses on that street, the

zems 560

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

【LeetCode】198. House Robber

198. House Robber Description: 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 ea...

Microstrong 3702

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

LeetCode 198. House Robber

House Robber - LeetCode 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 the...

zhaohaibo的博客 399

LeetCode House Robber II

原题链接在这里:https://leetcode.com/problems/house-robber-ii/ 是House Robber的进阶题。这里第一家和最后一家不能同时偷,所以像House Robber建立一个res1, 把从第一家到倒数第二家能偷最大值依次存入res1, 再建立一个res2, 把第二家到最后一家能偷最大值依次存入res2. 返回res1最后一个值 和res2最后一个值

Dylan_Java_NYC的博客 592

Leetcode: House Robber 系列

Leetcode: House Robber 系列这个系列包括以下题目: - 198. House Robber - 213. House Robber II - 337. House Robber III

qq_16318319的博客 437

[leetcode]动态规划之House Robber II

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

crabstew的博客 276

[leetcode] 337. House Robber III 解题报告

题目链接: https://leetcode.com/problems/house-robber-iii/ The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "root." Besides the root,

小榕流光的专栏 785

[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

LeetCode-198. House Robber [C++][Java]

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 systems connected andit will automatica

贫道绝缘子的博客 370

Leetcode337. House Robber III

打家劫舍的树形版本,限制条件是相连的节点不能一起偷

magic_jiayu的博客 207

[LeetCode] 198. House Robber

原题链接: https://leetcode.com/problems/house-robber/ 1. 题目介绍 You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint sto...

Ber03的博客 217

LeetCode //C - 337. House Robber III

【代码】LeetCode //C - 337. House Robber III。

Made in Code 1094
上一篇: Windows下C/C++连接MySQL
下一篇: vs2010 error link2019无法解析的外部符号
FightForProgrammer
博客等级 码龄13年 239粉丝 75原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值