当前位置:网站首页>LeetCode213. House raiding II
LeetCode213. House raiding II
2022-06-28 21:04:00 【Yuyy】
This paper is finally updated at 484 Days ago, , The information may have developed or changed .
One 、 Ideas
Compare it with the trade-offs , You can't steal the first house and the last house at the same time . There are more choices , Or steal the first one , Or steal the last home . The dynamic programming problem is to find out the choice among the questions , Get the State .
Two 、 problem
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 that can be stolen .
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 = [0]
Output :0Tips :
1 <= nums.length <= 1000 <= nums[i] <= 1000
Related Topics
- Dynamic programming
\n
- 475
- 0
3、 ... and 、 Code
public int rob(int[] nums) {
if (nums.length == 0) {
return 0;
}
if (nums.length == 1) {
return nums[0];
}
return Math.max(rangeRob(nums, 0, nums.length - 2)
, rangeRob(nums, 1, nums.length - 1));
}
public int rangeRob(int[] nums, int start, int end) {
int[][] arr = new int[nums.length][2];
arr[start][1] = nums[start];
arr[start][0] = 0;
for (int i = start + 1; i <= end; i++) {
arr[i][1] = Math.max(arr[i - 1][0] + nums[i], arr[i - 1][1]);
arr[i][0] = Math.max(arr[i - 1][0], arr[i - 1][1]);
}
return Math.max(arr[end][0], arr[end][1]);
}Post Views: 249
边栏推荐
- Leetcode daily question - 324 Swing sort II
- 题解 Andy s First Dictionary(UVa10815)紫书P112set的应用
- MongoDB——副本集与分片
- Alibaba cloud MSE full link grayscale solution practice based on Apache apisik
- with torch. no_ Grad(): reason for using
- Leetcode 36. Effective Sudoku (yes, once)
- Analysis of variance
- Globalsign's Pan domain SSL certificate
- rapid ssl通配符证书八百一年是正版吗
- postman简介与安装步骤
猜你喜欢

Win 10 create a gin framework project

Ehcache configuration data, convenient for self checking

接口测试流程

MongoDB——副本集与分片

The principle and source code analysis of Lucene index construction

Apisik helps Middle East social software realize localized deployment

Query rewriting for opengauss kernel analysis

题解 Pie(POJ3122)超详细易懂的二分入门

API 网关 Apache APISIX 助力雪球双活架构演进

Data standardization processing
随机推荐
LeetCode每日一题——324. 摆动排序 II
How to add logs to debug anr problems
[try to hack] cobalt strike (I)
Various types of long
LeetCode122. 买卖股票的最佳时机II
with torch.no_grad():的使用原因
LeetCode每日一题——522. 最长特殊序列 II
【学习笔记】聚类分析
Why does next() in iterator need to be forcibly converted?
with torch. no_ Grad(): reason for using
Understanding of incomplete types
Explanation of memory dump triggered by software watchdog and anr
Anr no response introduction
oracle delete误删除表数据后如何恢复
How to understand the fast iteration of cloud native database?
Embedded dynamic Arabic string conversion LCD display string [thanks for Jianguo ambition]
认识Web自动化测试
Leetcode daily question - 710 Random numbers in the blacklist
【读书会第13期】视频文件的封装格式
The blocks problem (uva101) Purple Book p110vector application