当前位置:网站首页>力扣(LeetCode)198. 打家劫舍(2022.07.17)
力扣(LeetCode)198. 打家劫舍(2022.07.17)
2022-07-18 04:28:00 【ChaoYue_miku】
你是一个专业的小偷,计划偷窃沿街的房屋。每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警。
给定一个代表每个房屋存放金额的非负整数数组,计算你 不触动警报装置的情况下 ,一夜之内能够偷窃到的最高金额。
示例 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 。
提示:
1 <= nums.length <= 100
0 <= nums[i] <= 400
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/house-robber
方法一:动态规划
C++提交内容:
class Solution {
public:
int rob(vector<int>& nums) {
if (nums.empty()) {
return 0;
}
int size = nums.size();
if (size == 1) {
return nums[0];
}
int first = nums[0], second = max(nums[0], nums[1]);
for (int i = 2; i < size; i++) {
int temp = second;
second = max(first + nums[i], second);
first = temp;
}
return second;
}
};
边栏推荐
- Pl/sql cursor
- ,其的低电平期间,这个从沿使得指令数做出相应因为模型
- How to synchronize the local database to the cloud database in real time
- Which securities company is good for opening an account for buying stocks by mobile phone? Which is safer
- LeetCode 6121. Query the number with the smallest k after cutting the number
- js小案例
- Notes on the combined use of ftxui keys and ros2 cli (turnlesim+teleop)
- AutoJs学习-实现图片剪切
- PL/SQL 游标
- 如何把本地的数据库实时同步到云数据库
猜你喜欢
Pyqt5 case of weather forecast
WPF---设计一个简单的“在线教育系统”界面
ERP系统品牌
模板与泛型编程之引用类型的移除和增加
LeetCode 6121. Query the number with the smallest k after cutting the number
LeetCode 565. 数组嵌套
Summary of in-depth learning series
Enterprise architecture is the "site skill" of Engineering Students
2021 OWASP TOP 6-10 合集
II Machine level representation and operation of data
随机推荐
[kotlin] MVVM mode combined with the initial use and learning of sharedflow (including code analysis)
【英雄哥七月集训】第 17天:广度优先搜索
接口能够自动生e等异步接入或者重新机,
网络入门——企业组网介绍&网络基本知识
"Schedulemaster" can be used quickly Net core based open source distributed task scheduling platform
WPF---设计一个简单的“在线教育系统”界面
【每日一题】565. 数组嵌套
2021 OWASP TOP 5: 安全配置错误
FL Studio20.9水果个人工作室必备DAW编曲制作
redis使用队列进行消费
ORACLE: how to get the current schema name | accessing a table without specifying the schema name
聪明人离职,怎么能不得罪人?
如何把本地的数据库实时同步到云数据库
网络入门—家庭组网介绍&基本网络知识
对接恒生极速行情丨DolphinDB NSQ 插件使用教程
input数字自动分割Js插件
[转载] 6. TensorRT 进阶用法
Ml: introduction, principle, application method and detailed introduction of classic cases of life / sp-life
ML之LIME:SP-LIME的简介、原理、使用方法、经典案例之详细攻略
国内主流的ERP软件大盘点