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

    198. House Robber

    Total Accepted: 45873 Total Submissions: 142855 Difficulty: Easy

    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 nums_size = nums.size();
            int max_money = 0;
            
            vector<int>dp(nums_size,0);
            
            for(int i=0;i<nums_size;++i){
                int m = 0;
                for(int j=i-2;j>=0;j--){
                    m = max(m,dp[j]);
                }
                dp[i] = m+nums[i];
                max_money = max(max_money,dp[i]);
            }
            return max_money;
        }
        
    };
    
    /**
    dp[i] = max(dp[i-2],dp[i-3]...dp[1])+nums[i];
    [9,8,9,20,8]
    [1,2,3,55,54,2]
    */
     
     

    213. House Robber II

    Total Accepted: 18274 Total Submissions: 63612 Difficulty: Medium

    Note: This is an extension of House Robber.

    After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, the security system for these houses remain the same as for those in the previous street.

    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.

    /**
    dp[i] = max(dp[i-2],dp[i-3]...dp[1])+nums[i];
    [9,8,9,20,8]
    [1,2,3,55,54,2]
    */
    class Solution {
    public:
        int rob(vector<int>& nums,int start,int end) {
            if(end<=start) return 0;
            int nums_size = end-start;
            int max_money = 0;
            
            vector<int>dp(nums_size,0);
            int k = 0;
            cout<<"start="<<start<<" end="<<endl;
            for(int i=start;i<end;++i){
                int m = 0;
                for(int j=k-2;j>=0;j--){
                    m = max(m,dp[j]);
                }
                dp[k] = m+nums[i];
                cout<<"dp["<<(k)<<"]="<<dp[k]<<endl;
                max_money = max(max_money,dp[k]);
                k++;
            }
            
            return max_money;
        }
        int rob(vector<int>& nums) {
            int nums_size = nums.size();
            return nums_size==1 ? nums[0] : max(rob(nums,0,nums_size-1),rob(nums,1,nums_size));
        }
    };
  • 相关阅读:
    洛咕 P4474 王者之剑
    CF708D Incorrect Flow
    CF802C Heidi and Library (hard)
    CF434D Nanami's Power Plant
    洛咕3312 [SDOI2014]数表
    洛咕 P3704 [SDOI2017]数字表格
    洛咕 P3706 [SDOI2017]硬币游戏
    洛咕 P4491 [HAOI2018]染色
    Luogu4240 毒瘤之神的考验 莫比乌斯反演、根号分治
    UOJ269 清华集训2016 如何优雅地求和 下降幂多项式、NTT
  • 原文地址:https://www.cnblogs.com/zengzy/p/5060463.html
Copyright © 2011-2022 走看看