当前位置:网站首页>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;
}
}
边栏推荐
- ByteDance Android gold, silver and four analysis, Android interview question app
- Opportunity interview experience summary
- PHP realizes wechat applet face recognition and face brushing login function
- Have fun | latest progress of "spacecraft program" activities
- AutoLISP series (1): function function 1
- Horizontal and vertical centering method and compatibility
- Read PG in data warehouse in one article_ stat
- Deep listening array deep listening watch
- 值得一看,面试考点与面试技巧
- time标准库
猜你喜欢
正在准备面试,分享面经
QT 图片背景色像素处理法
skimage学习(3)——Gamma 和 log对比度调整、直方图均衡、为灰度图像着色
掌握这套精编Android高级面试题解析,oppoAndroid面试题
[designmode] proxy pattern
Spark Tuning (III): persistence reduces secondary queries
QT中自定义控件的创建到封装到工具栏过程(二):自定义控件封装到工具栏
Direct dry goods, 100% praise
Binary search tree (features)
AutoLISP series (1): function function 1
随机推荐
DNS 系列(一):为什么更新了 DNS 记录不生效?
字节跳动Android金三银四解析,android面试题app
LeetCode 1981. 最小化目标值与所选元素的差 每日一题
掌握这个提升路径,面试资料分享
Personal notes of graphics (1)
Direct dry goods, 100% praise
直接上干货,100%好评
Geoserver2.18 series (5): connect to SQLSERVER database
Deep listening array deep listening watch
AutoLISP series (1): function function 1
无法将“pip”项识别为 cmdlet、函数、脚本文件或可运行程序的名称
OpenGL personal notes
QT 图片背景色像素处理法
DAPP defi NFT LP single and dual currency liquidity mining system development details and source code
three. JS create cool snow effect
应用在温度检测仪中的温度传感芯片
LeetCode 1043. 分隔数组以得到最大和 每日一题
Introduction to ThinkPHP URL routing
网关Gateway的介绍与使用
Opencv configuration 2019vs