当前位置:网站首页>213. house raiding II
213. house raiding II
2022-06-26 00:46:00 【Zzu dish】
raid homes and plunder houses II
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
reflection
This question only needs to use 198 raid homes and plunder houses Before method acquisition of n-1 Maximum number of thefts per day and after n-1 Maximum number of thefts per day , Just take the maximum .
class Solution {
public int rob(int[] nums) {
if(nums.length==1) return nums[0];
if (nums.length==2) return Math.max(nums[0],nums[1]);
int a=robTools(Arrays.copyOfRange(nums,0,nums.length-1));
int b=robTools(Arrays.copyOfRange(nums,1,nums.length));
return Math.max(a,b);
}
public int robTools(int[] nums) {
if(nums.length==1) return nums[0];
//step 1 establish dp Array
int[] dp=new int[nums.length];
// step 2 initialization dp Array
dp[0] = nums[0];
if(nums[1]>nums[0])
dp[1] = nums[1];
else dp[1] = dp[0];
if(dp.length==2) return dp[1];
// step 3 perfect dp Array
for (int i=2;i<dp.length;i++){
dp[i] = Math.max(dp[i-1],dp[i-2]+nums[i]);
}
return dp[dp.length-1];
}
}
边栏推荐
- 渗透工具-Burpsuite
- CaMKIIa和GCaMP6f是一樣的嘛?
- No executorfactory found to execute the application
- Understanding of prototypes and prototype chains
- 19c installing PSU 19.12
- 实现异步的方法
- EBS r12.2.0 to r12.2.6
- Research and development practice of Kwai real-time data warehouse support system
- Learn to identify follow-up questions in dialogue Q & A
- How product managers control the progress of product development
猜你喜欢
CaMKIIa和GCaMP6f是一樣的嘛?
EBS r12.2.0 to r12.2.6
Penetration tool -burpsuite
Wireshark's analysis of IMAP packet capturing
leetcode.14 --- 最长公共前缀
mtb13_Perform extract_blend_Super{Candidate(PrimaryAlternate)_Unique(可NULL过滤_Foreign_index_granulari
SSL unresponsive in postman test
Ad20 (Altium designer) PCB highlight network
Display unassigned virtual address after easyconnect connection
CaMKIIa和GCaMP6f是一样的嘛?
随机推荐
Xiaohongshu microservice framework and governance and other cloud native business architecture evolution cases
Summary of common terms and knowledge in SMT chip processing industry
How to bypass SSL authentication
mtb13_ Perform extract_ blend_ Super{candidate (primaryalternate) \u unique (nullable filtering \foreign\index\granulati
Idea view unit test coverage
Circuit board edge removal - precautions for V-CUT splitting machine
Analyze the five root causes of product development failure
[OEM special event] in the summer of "core cleaning", there are prize papers
DPVS fullnat mode kept
4 key points to help the product manage the project well
性能领跑云原生数据库市场!英特尔携腾讯共建云上技术生态
Resolve thread concurrency security issues
No executorfactory found to execute the application
Solution to SMT grape ball phenomenon
What do SMT operators do? operating duty?
Compile the telegraph desktop side (tdesktop) using vs2022
使用VS2022編譯Telegram桌面端(tdesktop)
Machine vision: illuminating "intelligence" and creating a new "vision" world
Phoenix index
事物/现象/事情/东西/情况/表象