当前位置:网站首页>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]);
}
}
边栏推荐
猜你喜欢

Chengxin University envi_ IDL third week class content 1: reading OMI data (HDF5 file) and output + parsing

VS Code 问题:launch:program‘...\.vscode\launch.exe‘ dose not exist

03 design of urban road dedusting and cooling system based on ZigBee

散列表、布隆过滤器、分布式一致性hash

池式组件之线程池篇

ENVI_IDL:批量重投影Modis Swath产品并指定范围输出为Geotiff格式+解析

Double Q-Learning理论基础及其代码实现【Pendulum-v0】

Frustratingly Simple Few-Shot Object Detection

Dueling DQN的理论基础及其代码实现【Pytorch + Pendulum-v0】

笔记一之IDL基础内容:常用数据类型_创建数组_类型转换_print输出_基本运算_关系运算
随机推荐
Uncaught syntaxerror: unexpected token '< is reported on the blank page of the H5 uniapp package‘
The code of dsaa related articles in the blog
gdb+vscode进行调试——release版本如何调试
MATLAB :Warning: the font “Times” is not available
DQN理论基础及其代码实现【Pytorch + CartPole-v0】
Vmware Tools最新安装教程(RHEL8)
Chengxin University envi_ IDL third week class content 1: reading OMI data (HDF5 file) and output + parsing
CMake常用命令
SAE j1708/j1587 protocol details
LeetCode:动态规划【基础题目求解】
ENVI_ Idl: reading of text files (mainly txt and CSV files)
Configure map reduce workflow in oozie
指針常量與常量指針愛恨情仇
DGC最佳实践:机密数据入湖,如何保证数据不被泄露?
Prohibit smart Safari from playing automatically when opening a web page
02 design of smart home system based on ZigBee
The differences and usage of cookies, localstorage and sessionstorage
Pointer constant and constant pointer love and hate
Double Q-Learning理论基础及其代码实现【Pendulum-v0】
ENVI_ Idl: batch splice the daily data of MODIS swath and output it in GeoTIFF format