当前位置:网站首页>LeetCode--打家劫舍问题
LeetCode--打家劫舍问题
2022-07-31 23:58:00 【华为云】
打家劫舍
你是一个专业的小偷,计划偷窃沿街的房屋。每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警。
给定一个代表每个房屋存放金额的非负整数数组,计算你 不触动警报装置的情况下 ,一夜之内能够偷窃到的最高金额。
示例 1:
输入:[1,2,3,1]输出:4解释:偷窃 1 号房屋 (金额 = 1) ,然后偷窃 3 号房屋 (金额 = 3)。 偷窃到的最高金额 = 1 + 3 = 4 。示例 2:
输入:[2,7,9,3,1]输出:12解释:偷窃 1 号房屋 (金额 = 2), 偷窃 3 号房屋 (金额 = 9),接着偷窃 5 号房屋 (金额 = 1)。 偷窃到的最高金额 = 2 + 9 + 1 = 12 。提示:
0 <= nums.length <= 1000 <= nums[i] <= 400
class Solution {private: int tryRob( vector<int> &nums, int index ) { if ( index >= nums.size() ) return 0; for( int i = index; i < nums.size(); i++ ) res = max( res, nums[i] + tryRob(nums, i + 2) ); return res; }public: int rob(vector<int>& nums) { return tryRob(nums, 0); }};记忆化搜索
class Solution {private: // memo[i] 表示考虑抢劫 nums[i..n]所能获得的最大收益 vector<int> memo; int tryRob( vector<int> &nums, int index ) { if ( index >= nums.size() ) return 0; if ( memo[index] != -1) return memo[index]; int res = 0; for( int i = index; i < nums.size(); i++ ) res = max( res, nums[i] + tryRob(nums, i + 2) ); memo[index] = res; return res; }public: int rob(vector<int>& nums) { memo = vector<int>(nums.size(), -1); return tryRob(nums, 0); }};动态规划
class Solution {public: int rob(vector<int>& nums) { int n = nums.size(); if ( n == 0) return 0; vector<int> memo(n, -1); memo[n-1] = nums[n-1]; for( int i = n - 2; i >= 0; i-- ) { for( int j = i; j < n; j++ ) { memo[i] = max( memo[i], nums[j] + (j+2 < n ? memo[j+2] : 0) ); } } return memo[0]; }};改变对状态的定义:
考虑偷取[0…x]范围里的房子(函数的定义)
边栏推荐
- 编译型语言和解释型语言的区别
- date命令
- Google Earth Engine——Error: Image.clipToBoundsAndScale, argument ‘input‘: Invalid type的错误解决
- Binary tree non-recursive traversal
- 消息队列存储消息数据的MySQL表格
- Usage of mysql having
- SQL injection Less46 (injection after order by + rand() Boolean blind injection)
- Interview assault 69: TCP reliable?Why is that?
- IPD process terminology
- lua入门案例实战1234定义函数与标准函数库功能
猜你喜欢
![[MATLAB project combat] LDPC-BP channel coding](/img/37/4777e4d05cb2dbb1865f1d05ae9878.png)
[MATLAB project combat] LDPC-BP channel coding

TFC CTF 2022 WEB Diamand WriteUp
I don't know what to do with sync issues

嵌入式开发没有激情了,正常吗?

cobaltstrike
不知道该怎么办的同步问题

cobaltstrike

2022-07-31:给出一个有n个点,m条有向边的图, 你可以施展魔法,把有向边,变成无向边, 比如A到B的有向边,权重为7。施展魔法之后,A和B通过该边到达彼此的代价都是7。 求,允许施展一次魔法

TFC CTF 2022 WEB Diamand WriteUp

程序进程和线程(线程的并发与并行)以及线程的基本创建和使用
随机推荐
不知道该怎么办的同步问题
面试题:实现死锁
Interview Blitz 69: Is TCP Reliable?Why?
Xinao Learning Plan The Road to Informatics Competition (2022.07.31)
The uniapp applet checks and prompts for updates
lua入门案例实战123DIY
游戏安全03:缓冲区溢出攻击简单解释
字符编码和浮点型计算精度丢失问题
助力数字政府建设,中科三方构建域名安全保障体系
网易云信圈组上线实时互动频道,「破冰」弱关系社交
Pylint检查规则中文版
C# Rectangle basic usage and picture cutting
SQL injection Less54 (limited number of SQL injection + union injection)
The difference between /usr/local/bin and /usr/bin
SQL注入 Less47(报错注入) 和Less49(时间盲注)
【Acwing】第62场周赛 题解
继承的注意事项
Flutter教程之 02 Flutter 桌面程序开发入门教程运行hello world (教程含源码)
浏览器下载快捷方式到桌面(PWA)
SQL注入 Less46(order by后的注入+rand()布尔盲注)