当前位置:网站首页>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));
}
};
边栏推荐
- Hiring a WordPress Developer: 4 Practical Ways
- bool Frame::PosInGrid(const cv::KeyPoint &kp, int &posX, int &posY)
- LeetCode Brushing Diary: 74. Searching 2D Matrix
- typescript29-枚举类型的特点和原理
- C语言之插入字符简单练习
- YGG 公会发展计划第 1 季总结
- 三本毕业的我被腾讯拒绝了十四次,最终成功入职阿里
- LeetCode刷题日记:34、 在排序数组中查找元素的第一个和最后一个位置
- typescript33-typescript高级概述
- 3. Bean scope and life cycle
猜你喜欢
typescript32-ts中的typeof
typescript30 - any type
数据链路层的数据传输
6-24 exploit-vnc password cracking
Handwriting a blogging platform ~ Day 3
MySQL8 下载、启动、配置、验证
kubernetes之服务发现
【ORB_SLAM2】void Frame::ComputeImageBounds(const cv::Mat &imLeft)
Yunhe Enmo: Let the value of the commercial database era continue to prosper in the openGauss ecosystem
Constructor instance method inheritance of typescript38-class (implement)
随机推荐
Garbage Collector CMS and G1
ofstream,ifstream,fstream读写文件
Constructor of typescript35-class
【LeetCode每日一题】——103.二叉树的锯齿形层序遍历
typescript34-class的基本使用
3 Month Tester Readme: 4 Important Skills That Impacted My Career
Speed up your programs with bitwise operations
typeof in typescript32-ts
Byte taught me a hard lesson: When a crisis comes, you don't even have time to prepare...
Pcie the inbound and outbound
Day116. Shangyitong: Details of appointment registration ※
C语言之插入字符简单练习
Record the pits where an error occurs when an array is converted to a collection, and try to use an array of packaging types for conversion
AntPathMatcher使用
【LeetCode每日一题】——704.二分查找
Handwriting a blogging platform ~ Day 3
Effects of Scraping and Aggregation
手写一个博客平台~第三天
【ORB_SLAM2】void Frame::AssignFeaturesToGrid()
"NetEase Internship" Weekly Diary (2)