题目:

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. 如何使用《DB 查询分析器》高效地生成旬报货运量数据

    如何使用<DB 查询分析器>高效地生成旬报货运量数据 马根峰                    (广东联合电子服务股份有限公司, 广州 510300) 1      引言   中国本土 ...

  2. 看见的力量 – (II) 影响地图

    本文转自台湾的李智桦老师的博客,原文地址 Impact Mapping 真是令人惊艳的可视化工具.等你看完这篇文章,你会爱上它的. 典故 继2011年6月Example of specificatio ...

  3. Cocos2D实现上下滚动式状态窗口

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 有时候要显示的内容太多,我们无法在iOS设备的小屏幕上显示出来 ...

  4. Android开发学习之路--Activity之四种启动模式

    后天终于可以回家了,马上就要过年了,趁着年底打酱油的模式,就多学习学习,然后记录记录吧.关于Activity已经学习了七七八八了,还有就是Activity的四种启动模式了,它们分别为,standard ...

  5. Linux上程序调试的基石(2)--GDB

    3. GDB的实现 GDB是GNU发布的一个强大的程序调试工具,用以调试C/C++程序.可以使程序员在程序运行的时候观察程序在内存/寄存器中的使用情况.它的实现也是基于ptrace系统调用来完成的.  ...

  6. HDFS追本溯源:HDFS操作的逻辑流程与源码解析

    本文主要介绍5个典型的HDFS流程,这些流程充分体现了HDFS实体间IPC接口和stream接口之间的配合. 1. Client和NN Client到NN有大量的元数据操作,比如修改文件名,在给定目录 ...

  7. Oracle Apps DBA 常用命令

    数据库启动监听 addlnctl.sh start instance 启动数据库 addbctl.sh start 启动应用服务器 adstrtal.sh 停止应用服务器 adstpall.sh -- ...

  8. 【Unity Shaders】Lighting Models —— 衣服着色器

    本系列主要参考<Unity Shaders and Effects Cookbook>一书(感谢原书作者),同时会加上一点个人理解或拓展. 这里是本书所有的插图.这里是本书所需的代码和资源 ...

  9. Git版本控制:Git高级教程

    http://blog.csdn.net/pipisorry/article/details/50669350 Git有很多命令行参数,使用起来非常方便.可以运行 man git log ,来看一下这 ...

  10. 【一天一道LeetCode】#102. Binary Tree Level Order Traversal

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...