当前位置:网站首页>LeetCode 213. Robbery II (2022.08.01)
LeetCode 213. Robbery II (2022.08.01)
2022-08-02 02:00:00 【ChaoYue_miku】
你是一个专业的小偷,计划偷窃沿街的房屋,每间房内都藏有一定的现金.这个地方所有的房屋都 围成一圈 ,这意味着第一个房屋和最后一个房屋是紧挨着的.同时,相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警 .
给定一个代表每个房屋存放金额的非负整数数组,计算你 在不触动警报装置的情况下 ,今晚能够偷窃到的最高金额.
示例 1:
输入:nums = [2,3,2]
输出:3
解释:你不能先偷窃 1 号房屋(金额 = 2),然后偷窃 3 号房屋(金额 = 2), 因为他们是相邻的.
示例 2:
输入:nums = [1,2,3,1]
输出:4
解释:你可以先偷窃 1 号房屋(金额 = 1),然后偷窃 3 号房屋(金额 = 3).
偷窃到的最高金额 = 1 + 3 = 4 .
示例 3:
输入:nums = [1,2,3]
输出:3
提示:
1 <= nums.length <= 100
0 <= nums[i] <= 1000
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/house-robber-ii
方法一:动态规划
C++提交内容:
class Solution {
public:
int robRange(vector<int>& nums, int start, int end) {
int first = nums[start], second = max(nums[start], nums[start + 1]);
for (int i = start + 2; i <= end; i++) {
int temp = second;
second = max(first + nums[i], second);
first = temp;
}
return second;
}
int rob(vector<int>& nums) {
int length = nums.size();
if (length == 1) {
return nums[0];
} else if (length == 2) {
return max(nums[0], nums[1]);
}
return max(robRange(nums, 0, length - 2), robRange(nums, 1, length - 1));
}
};
边栏推荐
猜你喜欢
Kubernetes之本地存储
Some insights from 5 years of automated testing experience: UI automation must overcome these 10 pits
个人博客系统项目测试
Constructor instance method inheritance of typescript37-class (extends)
hash table
Byte taught me a hard lesson: When a crisis comes, you don't even have time to prepare...
力扣 1161. 最大层内元素和
『网易实习』周记(二)
一本适合职场新人的好书
Use baidu EasyDL implement factory workers smoking behavior recognition
随机推荐
待读书单列表
Multi-Party Threshold Private Set Intersection with Sublinear Communication-2021: Interpretation
6-24 exploit-vnc password cracking
Yunhe Enmo: Let the value of the commercial database era continue to prosper in the openGauss ecosystem
Constructor instance method inheritance of typescript37-class (extends)
ofstream,ifstream,fstream读写文件
Day115. Shangyitong: Background user management: user lock and unlock, details, authentication list approval
Speed up your programs with bitwise operations
【LeetCode Daily Question】——704. Binary Search
Constructor instance method of typescript36-class
云和恩墨:让商业数据库时代的价值在openGauss生态上持续繁荣
Fly propeller power space future PIE - Engine Engine build earth science
Chengdu openGauss user group recruit!
Image fusion based on weighted 】 and pyramid image fusion with matlab code
HSDC is related to Independent Spanning Tree
typescript38-class的构造函数实例方法继承(implement)
Ask God to answer, how should this kind of sql be written?
搜罗汇总的效应
[ORB_SLAM2] void Frame::ComputeImageBounds(const cv::Mat & imLeft)
【ORB_SLAM2】void Frame::ComputeImageBounds(const cv::Mat &imLeft)