Letcode House Robber 시리즈

2282 단어 동적 기획

Leetcode #198 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.
class Solution {
public:
    int rob(vector &num) {
        int n = num.size();
        if(n == 0) return 0;
        if(n == 1) return num[0];

        vector dp(n, 0);
        dp[0] = num[0];  dp[1] = max(num[0], num[1]);
        for(int i = 2; i < n; i ++)
            dp[i] = max(dp[i-2] + num[i], dp[i-1]);
        return dp[n-1];
    }
};

 
Leetcode #213 House Robber II
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.
class Solution {
public:
    int rob(vector &num) {
        int n = num.size();
        if(n == 0) return 0;
        if(n == 1) return num[0];
        if(n == 2) return max(num[0], num[1]);

        vector dp(n, 0);
        
        dp[1] = num[1]; dp[2] = max(num[1], num[2]);
        for(int i = 3; i < n; i ++)
            dp[i] = max(dp[i-2] + num[i], dp[i-1]);
            
        int ans =  dp[n-1];
        
        dp[0] = num[0]; dp[1] = max(num[0], num[1]);
        for(int i = 2; i < n - 1; i ++)
            dp[i] = max(dp[i-2] + num[i], dp[i-1]);
            
        ans = max(ans, dp[n-2]);
        return ans;
    }
};

좋은 웹페이지 즐겨찾기