当前位置:网站首页>LeetCode 213. Home raiding II daily question
LeetCode 213. Home raiding II daily question
2022-07-07 16:58:00 【@Little safflower】
Problem description
You are a professional thief , Plan to steal houses along the street , There is a certain amount of cash in every room . All the houses in this place are Make a circle , This means that the first house and the last house are next to each other . meanwhile , Adjacent houses are equipped with interconnected anti-theft system , If two adjacent houses are broken into by thieves on the same night , The system will automatically alarm .
Given an array of non negative integers representing the storage amount of each house , Count you Without triggering the alarm device , The maximum amount you can steal tonight .
Example 1:
Input :nums = [2,3,2]
Output :3
explain : You can't steal first 1 House No ( amount of money = 2), And then steal 3 House No ( amount of money = 2), Because they are next to each other .
Example 2:Input :nums = [1,2,3,1]
Output :4
explain : You can steal first 1 House No ( amount of money = 1), And then steal 3 House No ( amount of money = 3).
Maximum amount stolen = 1 + 3 = 4 .
Example 3:Input :nums = [1,2,3]
Output :3
Tips :
1 <= nums.length <= 100
0 <= nums[i] <= 1000source : Power button (LeetCode)
link :https://leetcode.cn/problems/house-robber-ii
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Java
class Solution {
public int rob(int[] nums) {
int n = nums.length;
if(n == 1) return nums[0];
if(n == 2) return Math.max(nums[0],nums[1]);
return Math.max(getBenefit(nums,0,n - 2),getBenefit(nums,1,n - 1));
}
public int getBenefit(int[] nums,int left,int right){
int first = nums[left];
int second = Math.max(nums[left],nums[left + 1]);
for(int i = left + 2;i <= right;i++){
int temp = second;
second = Math.max(second,nums[i] + first);
first = temp;
}
return second;
}
}
边栏推荐
- Temperature sensor chip used in temperature detector
- 在哪个期货公司开期货户最安全?
- Cesium (4): the reason why gltf model is very dark after loading
- Talk about the realization of authority control and transaction record function of SAP system
- spark调优(三):持久化减少二次查询
- 字节跳动Android面试,知识点总结+面试题解析
- [PHP] PHP interface inheritance and interface multi inheritance principle and implementation method
- OpenGL personal notes
- Sort out several important Android knowledge and advanced Android development interview questions
- 射线与OBB相交检测
猜你喜欢
【MySql进阶】索引详解(一):索引数据页结构
二叉搜索树(特性篇)
爬虫(17) - 面试(2) | 爬虫面试题库
Pycharm terminal enables virtual environment
Master this set of refined Android advanced interview questions analysis, oppoandroid interview questions
Sort out several important Android knowledge and advanced Android development interview questions
正在准备面试,分享面经
Lowcode: four ways to help transportation companies enhance supply chain management
如何快速检查钢网开口面积比是否符合 IPC7525
Vs2019 configuration matrix library eigen
随机推荐
Master this promotion path and share interview materials
"The" "PIP" "entry cannot be recognized as the name of a cmdlet, function, script file, or runnable program."
【DesignMode】享元模式(Flyweight Pattern)
全网“追杀”钟薛高
第九届 蓝桥杯 决赛 交换次数
1亿单身男女“在线相亲”,撑起130亿IPO
最新Android面试合集,android视频提取音频
01tire+ chain forward star +dfs+ greedy exercise one
Three. JS series (3): porting shaders in shadertoy
最新阿里P7技术体系,妈妈再也不用担心我找工作了
LeetCode 1626. 无矛盾的最佳球队 每日一题
skimage学习(2)——RGB转灰度、RGB 转 HSV、直方图匹配
[designmode] proxy pattern
Set the route and optimize the URL in thinkphp3.2.3
打造All-in-One应用开发平台,轻流树立无代码行业标杆
直接上干货,100%好评
使用JSON.stringify()去实现深拷贝,要小心哦,可能有巨坑
Arduino 控制的双足机器人
正在准备面试,分享面经
Usage of config in laravel