当前位置:网站首页>Leetcode 198:House Robber
Leetcode 198:House Robber
2022-07-19 02:42:00 【Little sun who wants to be a program yuan】
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.
Example 1:
Input: [1,2,3,1] Output: 4 Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3). Total amount you can rob = 1 + 3 = 4.
Example 2:
Input: [2,7,9,3,1] Output: 12 Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1). Total amount you can rob = 2 + 9 + 1 = 12.
answer :
Pure recursion :
class Solution {
public int rob(int[] arr) {
return search(arr,arr.length-1);
}
public int search(int[] arr,int index){
if(index < 0)
return 0;
else{
int a = arr[index] + search(arr,index - 2);
int b = search(arr,index - 1);
return Math.max(a,b);
}
}
}
Save the calculated value first , Re recursion :
Mine:
class Solution {
public static int[] f = new int[1000];
public int rob(int[] nums) {
for(int i = 0;i < f.length;++i){
f[i] = -1;
}
return search(nums, nums.length-1);
}
public int search(int[] nums,int index){
if(index < 0)
return 0;
if(f[index] >= 0)
return f[index];
else{
int a = nums[index] + search(nums,index - 2);
int b = search(nums,index - 1);
f[index] = Math.max(a, b);
return f[index];
}
}
}
Better Solution:
class Solution {
public int rob(int[] arr) {
if(arr.length <= 2){
if(arr.length == 2){
return Math.max(arr[0], arr[1]);
}
if(arr.length == 1){
return arr[0];
}
if(arr.length == 0) {
return 0;
}
//return Math.max(arr[0],arr[1]);
}
arr[2] = Math.max(arr[2], arr[2] + arr[0]);
for(int i = 3; i < arr.length; i++) {
int max = arr[i];
int n = Math.max(arr[i] + arr[i-2], arr[i] + arr[i-3]);
arr[i] = Math.max(max,n);
}
return Math.max(arr[arr.length - 1], arr[arr.length - 2]);
}
}
边栏推荐
- 安装.NET提示“无法建立到信任根颁发机构的证书链”(方法简单有下载地址)
- 脏读、幻读、不可重复读
- Simple use case writing specification
- 2022最新软件测试工具大全
- JMeter response time test component & multi interface concurrency
- 树状数组与ST表
- WINRAR命令拷贝指定文件夹为压缩文件,调用计划任务进行备份。
- 2022 latest software testing tools
- 通过Xshell7使用rz,sz命令上传下载文件
- [tools] unity quickly starts to make the artifact tilemap of 2D and 2.5D games
猜你喜欢
Sword finger offer 48 The longest substring without repeated characters
WINRAR命令拷贝指定文件夹为压缩文件,调用计划任务进行备份。
[unity Editor Extension] displays the memory size of all files in the resource directory
JMeter response time test component & multi interface concurrency
MySQL初探
【已解决】参考了本地mysql忘记密码后, [Server] --initialize specified but the data directory has files in it. Aborti
Static routing (detailed)
C语言回调函数 & sprinf 实际应用一例
并发虚拟用户、RPS、TPS的解读
jmeter连接数据库的方法
随机推荐
MySQL初探
jmeter连接数据库的方法
Shell脚本for、while循环语句、猜价格小游戏
Array Transformer-分块思想
Zabbix6.0通过iDRAC,IMM2监控DELL,IBM服务器硬件
Uni app wechat applet ordering system [another order] page Jump
Nmon使用方法
2022.6.28-database-1 Isolation level of database
网络层协议和IP数据包的格式(详解)
Sword finger offer 53 - I. find the number I in the sorted array
性能之流量回放
Flyway的SaaS多租户实现方案
Use JMeter to test services based on websocket protocol
Test points of login function
Understand inheritance, polymorphism, abstraction and their concepts
Shell脚本case分支语句、扒匿名登录FTP的max地址
YUM仓库服务与PXE自动部署系统
Performance test implementation specification Guide
Metersphere is based on JMeter distributed performance pressure testing platform
Attack and defense the world ---- shrink