zoukankan      html  css  js  c++  java
  • 198. House Robber

    1. 问题描述

    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.
    Tags: Dynamic Programming
    Similar Problems: (M) Maximum Product Subarray (M) House Robber II (M) Paint House (E) Paint Fence (M) House Robber III

    2. 解题思路

    • 动态规划

    3. 代码

     1 class Solution {
     2 public:
     3     int rob(vector<int>& nums)
     4     {
     5         int vSize = nums.size();
     6         if (0 == vSize)
     7         {
     8             return 0;
     9         }
    10         int Rob_last = 0;
    11         int UnRob_last = 0;    
    12         int Rob = 0;
    13         int UnRob = 0;
    14 
    15         for (int i=0; i<vSize; i++)
    16         {
    17             Rob = nums[i] + UnRob_last;
    18             UnRob = Rob_last > UnRob_last ? Rob_last : UnRob_last;
    19 
    20             UnRob_last = UnRob;
    21             Rob_last = Rob;
    22         }
    23 
    24         return Rob > UnRob ? Rob : UnRob;
    25     }
    26 };

    4. 反思

  • 相关阅读:
    元数据 缓存 池 模式
    ContractPattern 面向面向契约模式
    第三方登录 ----转载自简书,作者 <<碧霄问鼎>>
    证书那些事
    导航(NavanavigationController)push和pop
    iOS app上传错误集锦(转载)
    Block 的基本用法
    正则表达式的用法 转载
    UIView
    UIDate(时间)
  • 原文地址:https://www.cnblogs.com/whl2012/p/5785928.html
Copyright © 2011-2022 走看看