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 };
     
     
  • 相关阅读:
    chown
    [NOI2010]航空管制
    批量kill 某个用户session
    【BZOJ2395】【Balkan 2011】Timeismoney 最小乘积生成树
    找出 alter system kill session ‘sid,serial#’ kill 掉的数据库会话对应进程
    [NOI2016]优秀的拆分
    Oracle12C查询自建用户(非系统自带)
    查询包含某个字段的表
    [WC2011]最大XOR和路径
    监控慢SQL
  • 原文地址:https://www.cnblogs.com/LUO77/p/4996841.html
Copyright © 2011-2022 走看看