当前位置:网站首页>LeetCode--The problem of robbery
LeetCode--The problem of robbery
2022-08-01 00:02:00 【HUAWEI CLOUD】
打家劫舍
你是一个专业的小偷,计划偷窃沿街的房屋.每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警.
给定一个代表每个房屋存放金额的非负整数数组,计算你 不触动警报装置的情况下 ,一夜之内能够偷窃到的最高金额.
示例 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]; }};change the definition of state:
考虑偷取[0…x]范围里的房子(函数的定义)
边栏推荐
- Difference between first and take(1) operators in NgRx
- 周总结
- When can I use PushGateway
- To help the construction of digital government, the three parties of China Science and Technology build a domain name security system
- 简单的vim配置
- SQL injection Less46 (injection after order by + rand() Boolean blind injection)
- SQL注入 Less47(报错注入) 和Less49(时间盲注)
- date命令
- 【Acwing】The 62nd Weekly Game Solution
- 2022年CSP-J1 CSP-S1 第1轮初赛 报名指南
猜你喜欢

编译型语言和解释型语言的区别

虹科分享|如何用移动目标防御技术防范未知因素

游戏安全03:缓冲区溢出攻击简单解释

Advanced Algebra _ Proof _ Any matrix is similar to an upper triangular matrix

IJCAI2022 | 代数和逻辑约束的混合概率推理

自动化机器学习pycaret: PyCaret Basic Auto Classification LightGBM
![[微服务]分布式事务解决方案-Seata](/img/a8/fc6c24e4d42dfb635bad786cc02164.png)
[微服务]分布式事务解决方案-Seata

力扣二叉树

【Acwing】第62场周赛 题解

(26) About menu of the top menu of Blender source code analysis
随机推荐
Mysql environment installation under Linux (centos)
EntityFramework保存到SQLServer 小数精度丢失
SQL injection Less47 (error injection) and Less49 (time blind injection)
Difference between first and take(1) operators in NgRx
Interview Question: Implementing Deadlocks
SVN server construction + SVN client + TeamCity integrated environment construction + VS2019 development
基于mysql的消息队列设计
逐步手撕轮播图3(保姆级教程)
新产品如何进行网络推广?
thymeleaf迭代map集合
TypeScript 的组件
如何撰写出一篇优质的数码类好物推荐文
嵌入式开发没有激情了,正常吗?
[微服务]分布式事务解决方案-Seata
vim的基本使用-命令模式
Design of Fire and Anti-theft System Based on Single Chip GSM
力扣二叉树
类和对象:中
如何导入 Golang 外部包并使用它?
Redis五种数据类型简介