当前位置:网站首页>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 - 522 Longest special sequence II
- openGauss内核分析之查询重写
- [book club issue 13] packaging format of video files
- Resilience4j retry source code analysis and retry index collection
- 接口用例设计
- APISIX 助力中东社交软件,实现本地化部署
- Ehcache配置资料,方便自己查
- Win 10 create a gin framework project
- How to analyze the relationship between enterprise digital transformation and data asset management?
- 题解 Andy s First Dictionary(UVa10815)紫书P112set的应用
猜你喜欢

Apisik helps Middle East social software realize localized deployment

openGauss内核分析之查询重写

Application practice | 1billion data second level correlation. Huolala's OLAP System Evolution Based on Apache Doris (with PPT download)

Lucene构建索引的原理及源代码分析

学习太极创客 — MQTT 第二章(七)ESP8266 MQTT 遗嘱应用

数据资产为王,如何解析企业数字化转型与数据资产管理的关系?

数据标准化处理

Win 10 create a gin framework project

如何使用 DataAnt 监控 Apache APISIX

不同框架的绘制神经网络结构可视化
随机推荐
RT-Thread线程同步与线程通信
Employee salary management system
Real number operation
Leetcode daily question - 710 Random numbers in the blacklist
API gateway Apache APIs IX helps the evolution of snowball dual active architecture
Apisik helps Middle East social software realize localized deployment
嵌入式中 动态阿拉伯语字符串 转换 LCD显示字符串【感谢建国雄心】
ref属性,props配置,mixin混入,插件,scoped样式
ANR分析--问题1
SaaS sales upgrade under the new situation | tob Master Course
大智慧上怎么进行开户啊, 安全吗
rapid ssl通配符证书八百一年是正版吗
Leetcode daily question - 324 Swing sort II
方 差 分 析
Keyword long
Web 自动化环境搭建
LeetCode:合并两个有序链表_21
How to recover after Oracle delete accidentally deletes table data
Leetcode daily question - 522 Longest special sequence II
mysql-发生系统错误1067