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];
        }
    };
  • 相关阅读:
    站立会议第八天
    Servlet基础知识
    JSP基础知识
    JSP基础知识
    JDBC工具包
    JDBC
    MySQL
    ASP.NET程序代码优化的七个方面
    中小型软件项目开发一般流程建议
    理解九种图
  • 原文地址:https://www.cnblogs.com/qionglouyuyu/p/5050895.html
Copyright © 2011-2022 走看看