题目:

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.

思路:

  • 本质是求一个非负整数数组a【n】的非相邻元素的最大和。
  • 这一类问题要用动态规划的思路,把数组的长度,作为每一步。设置数组count【n】作为数组的每一步的最大的和。存在推倒关系:count【n】 = max(count【n-1】,(count【n-2】+a【n】));

    -

代码:

public class Solution {
    public int rob(int[] nums) {
        int[] count = null;
        int n = nums.length;
        if(n == 0){
            return 0;
        }
        if(n == 1){
            return nums[0];
        }
        if(n > 1){
            count = new int[n];
        }
        count[0] = nums[0];
        if(nums[1] > nums[0]){
            count[1] = nums[1];
        }else{
            count[1] = nums[0];
        }
        for(int i = 2;i < n;i++){
            if((count[i-2]+nums[i]) > count[i-1]){
                count[i] = count[i-2]+nums[i];
            }else{
                count[i] = count[i-1];
            }
        }
        return count[n-1];
    }
}

LeetCode之旅(22)-House Robber的更多相关文章

  1. leetcode之旅(11)-Integer to Roman

    题目描述: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range fr ...

  2. LeetCode之旅(13)-Valid Anagram

    题目介绍: Given two strings s and t, write a function to determine if t is an anagram of s. For example, ...

  3. LeetCode之“动态规划”:House Robber && House Robber II

    House Robber题目链接 House Robber II题目链接 1. House Robber 题目要求: You are a professional robber planning to ...

  4. LeetCode之旅(18)-Happy Number

    题目 Write an algorithm to determine if a number is "happy". A happy number is a number defi ...

  5. &lt;LeetCode OJ&gt; 337. House Robber III

    Total Accepted: 1341 Total Submissions: 3744 Difficulty: Medium The thief has found himself a new pl ...

  6. [算法学习]开始leetcode之旅

    在此记录一下用javascript刷leetcode的过程,每天都要坚持! 1.Two Sum Given an array of integers, find two numbers such th ...

  7. SAM4E单片机之旅——22、GMAC和PHY的介绍与初始化

    网络通信的作用不用多说,而这次进行的工作即是对以太网通信过程中,需要用到的硬件部分进行初始化,也介绍了发送和接收数据的方法. 由于较为复杂,所以使用了ASF框架.但是也会对用到的库函数的实现做一个介绍 ...

  8. 【leetcode❤python】198. House Robber

    class Solution(object):    def rob(self, nums):        """        :type nums: List[in ...

  9. Leetcode题解(22)

    66. Plus One 题目 这题很简单,直接代码: class Solution { public: vector<int> plusOne(vector<int> &am ...

随机推荐

  1. Microsoft Dynamics CRM 2011 当您在 大型数据集上执行 RetrieveMultiple 查询很慢的解决方法

    症状 当您在 Microsoft Dynamics CRM 2011 年大型数据集上执行 RetrieveMultiple 查询时,您会比较慢. 原因 发生此问题是因为大型数据集缓存 Retrieve ...

  2. Dynamics CRM2015 非基础语言环境下产品无法新建的问题

    该现象出现在2015版本上,之前从没注意过这个问题不知道以前的版本是否存在. 我的安装包的基础语言是中文,第一张图有添加产品的按钮,切换到英文环境下后就没有了,一开始以为是系统做了隐藏处理,但用工具查 ...

  3. Cocos2D瓦块地图高清屏(retina)显示比例问题的解决

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 在Cocos2D的游戏编程里,常用到瓦块地图.而cocos2D ...

  4. 所谓 Spinner 组件

    Spinner组件在平常的开发过程中应该算是比较常用的隶属于Input Control的android控件了,所以我们很有必要掌握这一个知识点,下面我就来谈一谈我对这个组件的一些认识. 是什么? 下拉 ...

  5. Ext JS 6开发实例(一)

    很久没写文章了,主要原因和大家差不多,都要为生活奔忙,搞了两个小项目.这两个小项目很凑巧,都可以使用Ext JS来开发,这正是练习使用Ext JS 6的好机会,自然不会错过. 很多读者可能会问,为什么 ...

  6. install_driver(Oracle) failed: Can't load `.../DBD/Oracle/Oracle.so' for module DBD::Oracle

    Description This section is from the "Practical mod_perl " book, by Stas Bekman and Eric C ...

  7. 高性能nosql ledisdb设计与实现 (2):replication

    ledisdb现在已经支持replication机制,为ledisdb的高可用做出了保障. 使用 假设master的ip为10.20.187.100,端口6380,slave的ip为10.20.187 ...

  8. Git版本控制:Gitlab及Coding.net的使用

    http://blog.csdn.net/pipisorry/article/details/50709014 Gitlab介绍 GitLab是利用 Ruby on Rails 一个开源的版本管理系统 ...

  9. Android的资源类型和存储方式简介-android学习之旅(五十二)

    android资源的类型 android资源的存储方式

  10. 存储那些事儿(五):BTRFS文件系统之Btree结构详解

    Btree数据结构可以说是BTRFS文件系统的基础.它提供了一个通用的方式去存储不同的数据类型.它仅仅存储3个数据类型:key, item和block header. btrfs_header的定义如 ...