zoukankan      html  css  js  c++  java
  • 198. House Robber(Array; DP)

    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.

    class Solution {
    public:
        int rob(vector<int>& nums) {
            int size = nums.size();
            if(size == 0) return 0;
            if(size == 1) return nums[0];
            
            vector<int> maxMoney(size,0); //save the max robbed money until now
            maxMoney[0] = nums[0];
            maxMoney[1] = max(nums[0],nums[1]);
            for(int i = 2; i < size; i++){
                maxMoney[i] = max(maxMoney[i-2]+nums[i], maxMoney[i-1]);
            }
            
            return maxMoney[size-1];
        }
    };
  • 相关阅读:
    mysql基础命令(一)
    vue组件之间的通信
    wepy的使用
    mockjs中的方法(三)
    每周散记 20181022
    api资源
    三七
    画中画 视频合成
    每周散记 20180910
    linux文件权限多一个+啥意思
  • 原文地址:https://www.cnblogs.com/qionglouyuyu/p/5050895.html
Copyright © 2011-2022 走看看