zoukankan      html  css  js  c++  java
  • leetcode House Robber

    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.

    Credits:
    Special thanks to @ifanchu for adding this problem and creating all test cases. Also thanks to @ts for adding additional test cases.

    Subscribe to see which companies asked this question

     
    一道动态规划的题目,想了很久,没想出来。== 最后看陆草纯的代码的。http://www.cnblogs.com/ganganloveu/p/4417485.html
    最开始我很简单的想,因为都是非负数嘛,所以就是越多加越好,所以就试了奇数号的加起来求和,偶数号的加起来求和。= = 然而这样行不通的,例如{1,3,1,3,100},{2,1,1,2}
    后来看了tag是动态规划的题目,= = 动态规划真是难啊。就是想不明白。。。看了要好好学学动态规划了。。
     
     1 class Solution {
     2 public:
     3     int rob(vector<int>& nums) {
     4         int s=nums.size();
     5         if(s==0) return 0;
     6         if(s==1) return nums[0];
     7         vector<int> maxn(s,0);
     8         maxn[0]=nums[0];
     9         maxn[1]=max(nums[0],nums[1]);
    10         for(int i=2;i<s;i++){
    11             maxn[i]=max(maxn[i-2]+nums[i],maxn[i-1]);
    12         }
    13         return maxn[s-1];
    14         
    15     }
    16 };
     
     
  • 相关阅读:
    http数据返回值
    刷新 返回
    微信平台上遇到的bug
    iscroll修改
    iscroll
    a标签的herf和click事件
    ios9+xcode7 适配笔记
    xsd、wsdl生成C#类的命令行工具使用方法
    xcode更新,想想也是醉了
    关于#define预处理指令的一个问题
  • 原文地址:https://www.cnblogs.com/LUO77/p/4996841.html
Copyright © 2011-2022 走看看