当前位置:网站首页>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]范围里的房子(函数的定义)
边栏推荐
- 2022-07-31:给出一个有n个点,m条有向边的图, 你可以施展魔法,把有向边,变成无向边, 比如A到B的有向边,权重为7。施展魔法之后,A和B通过该边到达彼此的代价都是7。 求,允许施展一次魔法
- 一文概述:VPN的基本模型及业务类型
- leetcode:126. 单词接龙 II
- (26)Blender源码分析之顶层菜单的关于菜单
- LeetCode--打家劫舍问题
- mysql having的用法
- How to import a Golang external package and use it?
- 游戏安全03:缓冲区溢出攻击简单解释
- 面试突击69:TCP 可靠吗?为什么?
- Network security - crack WiFi through handshake packets (detailed tutorial)
猜你喜欢

【Acwing】The 62nd Weekly Game Solution

《ArchSummit:时代的呐喊,技术人听得到》
![[MATLAB project combat] LDPC-BP channel coding](/img/37/4777e4d05cb2dbb1865f1d05ae9878.png)
[MATLAB project combat] LDPC-BP channel coding

什么是客户画像管理?

什么是动态规划,什么是背包问题

Shell常用脚本:Nexus批量上传本地仓库增强版脚本(强烈推荐)

消息队列消息存储设计(架构实战营 模块八作业)

Flink 1.13(八)CDC

One line of code to solve CoreData managed object properties change in SwiftUI problem of animation effects

清华大学陈建宇教授团队 | 基于接触丰富机器人操作的接触安全强化学习框架
随机推荐
Named Entity Recognition - Model: BERT-MRC
面试突击69:TCP 可靠吗?为什么?
周总结
手写一个简单的web服务器(B/S架构)
逐步手撕轮播图3(保姆级教程)
景区手绘地图的绘制流程
如何撰写出一篇优质的数码类好物推荐文
日常--Kali开启SSH(详细教程)
内核对设备树的处理
Program processes and threads (concurrency and parallelism of threads) and basic creation and use of threads
编译型语言和解释型语言的区别
What is customer profile management?
网络安全--通过握手包破解WiFi(详细教程)
UOS - WindTerm use
【Acwing】The 62nd Weekly Game Solution
Difference between first and take(1) operators in NgRx
LeetCode--打家劫舍问题
Interview Blitz 69: Is TCP Reliable?Why?
Keil nRF52832 download failed
C# Rectangle基本用法和图片切割