当前位置:网站首页>Leetcode 198:House Robber
Leetcode 198:House Robber
2022-07-17 00:13:00 【想做程序媛的小太阳】
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.
解答:
纯递归:
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);
}
}
}先保存已计算值,再递归:
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]);
}
}
边栏推荐
- 基于蒙特卡洛的强化学习方法【附带代码实现】
- Foo bar 什么鬼?
- Allegro design entry CIS and OrCAD capture CIS relationship
- Original code, inverse code, complement code
- VS Code 问题:launch:program‘...\.vscode\launch.exe‘ dose not exist
- 字符串转换为整数
- Clion 安装以及中开发ROS实现自动提示补全
- gdb+vscode进行调试1——使用CMakelist文件进行编译和调试+附加进程调试
- Oozie 集成 Ssh
- 图像质量评估指标:SNR、PSNR、MSE和SSIM
猜你喜欢

二叉树的遍历

gdb+vscode进行调试5——gdb查看相关命令

【白话模电1】PN结与二极管

gdb+vscode进行调试2——gdb断点相关

Powerful chart component library scottplot

CAN协议通信

ENVI_ Idl: batch re projection of modisswath products (calling the secondary development interface) + parsing

SAE j1708/j1587 protocol details

ENVI_ Idl: batch splice the daily data of MODIS swath and output it in GeoTIFF format

VIM 配置文件
随机推荐
性能强悍的图表组件库 ScottPlot
Oozie integrated sh
Labelme正常启动,但无法打开
VIM profile
成信大ENVI_IDL第一周实验测试:数组的简单运算+详细解析
指針常量與常量指針愛恨情仇
搭建Ozzie环境
Original code, inverse code, complement code
DGC best practice: how to ensure that confidential data is not leaked when entering the lake?
06 design of smart electronic medicine box based on stm32
Oozie 集成 Sqoop
[cute new problem solving] sum of three numbers
Oozie integrated sqoop
ENVI_ Idl: read the NO2 column content of all OMI products and calculate the monthly average, quarterly average, annual average + analysis
图像质量评估指标:SNR、PSNR、MSE和SSIM
03基于ZigBee的城市道路除尘降温系统设计
Cookie和Session的区别
ENVI_ Idl: average calculation + analysis of MODIS swath products in batches
ENVI_ Idl: batch re projection of modisswath products (calling the secondary development interface) + parsing
Foo bar what the hell?