博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Leetcode198——House Robber
阅读量:3977 次
发布时间:2019-05-24

本文共 3002 字,大约阅读时间需要 10 分钟。

文章作者:Tyan

博客:  |   | 

1. 问题描述

ou 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.

2. 求解

方法一递归法

递归法如下图所示,从第0家开始,偷与不偷有两种情况,如果偷了则从第2家开始重复此过程,如果没偷,从第1家开始重复此过程,直至结束。

递归过程

public class Solution {    public int rob(int[] nums) {        if(nums.length == 0) {            return 0;        }        return robot(0, nums);    }    public int robot(int start, int[] nums) {        if(start >= nums.length) {            return 0;        }        int left = nums[start] + robot(start + 2, nums);        int right = 0 + robot(start + 1, nums);        int max = Math.max(left, right);        return max;    }}

Leetcode运行超时,说明存在优化空间。

方法二递归法+缓存

从图中可以看出,上述过程中会有重复的计算,例如从第二家开始偷的情况计算了两次,我们可以用一个HashMap将计算过的数据保存起来,去掉冗余计算。

public class Solution {    public static Map
cache = new HashMap
(); public int rob(int[] nums) { cache.clear(); if(nums.length == 0) { return 0; } return robot(0, nums); } public int robot(int start, int[] nums) { if(start >= nums.length) { return 0; } if(cache.containsKey(start)) { return cache.get(start); } int left = nums[start] + robot(start + 2, nums); int right = 0 + robot(start + 1, nums); int max = Math.max(left, right); cache.put(start, max); return max; }}

方法三:非递归

将递归改造成非递归。

public class Solution {    public static Map
cache = new HashMap
(); public int rob(int[] nums) { cache.clear(); if(nums.length == 0) { return 0; } int n = nums.length; cache.put(n-1, nums[n - 1]); for(int i = n - 2; i >=0; i--) { int a = nums[i] + (cache.containsKey(i + 2) ? cache.get(i + 2) : 0); int b = cache.containsKey(i + 1) ? cache.get(i + 1) : 0; int max = Math.max(a, b); cache.put(i, max); } return cache.get(0); }}

**方法四:**HashMap变为数组

为了节省空间,可以将HashMap变为数组,代码也可以进一步优化,去掉三目运算符。

public class Solution {    public int rob(int[] nums) {        if(nums.length == 0) {            return 0;        }        if(nums.length == 1) {            return nums[0];        }        if(nums.length == 2) {            return Math.max(nums[0], nums[1]);        }        int n = nums.length;        int[] cache = new int[n];        cache[n - 1] = nums[n - 1];        cache[n - 2] = Math.max(nums[n - 1], nums[n - 2]);        for(int i = n - 3; i >=0; i--) {            cache[i] = Math.max(nums[i] + cache[i + 2], cache[i + 1]);        }        return cache[0];    }   }

转载地址:http://zcwui.baihongyu.com/

你可能感兴趣的文章
C语言 回调函数
查看>>
c语言swap(a,b)值交换的4种实现方法
查看>>
C++小知识点
查看>>
【转载】zedboard中PL_GPIO控制(8个sw、8个leds)
查看>>
zedboard烧写程序到FLASH,用于QSPI Flash启动
查看>>
软件工程师,你必须知道的20个常识
查看>>
常用STL算法2_查找
查看>>
常用STL算法3_排序
查看>>
常用STL算法4_拷贝和替换
查看>>
STL综合案例
查看>>
O(logn)时间复杂度求Fibonacci数列
查看>>
Iterator_traits
查看>>
Zedboard中的SPI通信记录文档(已实现)
查看>>
Android 发布到google Play的app搜索不到问题的解决
查看>>
Flutter 网络请求之基于dio的简单封装
查看>>
Flutter UI基础 - 路由之Navigator详解
查看>>
Flutter UI基础 - Widgets 之 InkWell 和 Ink
查看>>
Spring - sentinel和hystrix比较
查看>>
Flutter 日期插件date_format 中文 国际化 及flutter_cupertino_date_picker
查看>>
Flutter 插件笔记 | 屏幕适配 flutter_screenutil
查看>>