当前位置:网站首页>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
边栏推荐
- The comprehensive application of the setstack computer (uva12096) Purple Book p116stl
- [book club issue 13] packaging format of video files
- ThreadLocal principle
- 题解 Pie(POJ3122)超详细易懂的二分入门
- How to open an account in great wisdom? Is it safe
- 软件watchdog和ANR触发memory dump讲解
- Leetcode daily question - 515 Find the maximum value in each tree row
- ANR无响应介绍
- 数据标准化处理
- Input and output character data
猜你喜欢

MySQL system error occurred 1067

How to analyze the relationship between enterprise digital transformation and data asset management?

我也差点“跑路”

The further application of Li Kou tree

APISIX 助力中东社交软件,实现本地化部署

不同框架的绘制神经网络结构可视化

视频号如何下载视频?来看超简单方法!

【笔记:模拟MOS集成电路】带隙基准(基本原理+电流模+电压模电路详解)

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

with torch.no_grad():的使用原因
随机推荐
No module named ‘PyEMD‘ ; Use plt figure()TypeError: ‘module‘ object is not callable
【学习笔记】因子分析
With a market value of $120billion, how did intuit, an old tax giant, do it?
嵌入式中 动态阿拉伯语字符串 转换 LCD显示字符串【感谢建国雄心】
Leetcode daily question - Sword finger offer II 091 Paint the house
Lucene构建索引的原理及源代码分析
2. integrate filter
Pie (poj3122) super detailed and easy to understand binary introduction
LeetCode117. 填充每个节点的下一个右侧节点指针_II
LeetCode121. 买卖股票的最佳时机
Comparisonchain file name sort
[learning notes] Introduction to principal component analysis
Visualization of neural network structure in different frames
Leetcode daily question - 30 Concatenate substrings of all words
【学习笔记】主成分分析法介绍
如何添加 logs来debug ANR 问题
LeetCode986. 区间列表的交集
How to understand the fast iteration of cloud native database?
Bitbucket 使用 SSH 拉取仓库失败的问题
List of domestic database directory