Simple dp solution 0ms accepted !!


  • 2
    R
    int rob(vector<int>& a) {
        
        if(a.empty())
            return 0;
            
        int n = a.size();
        
        int dp[n];
        
        dp[0] = a[0];
        dp[1] = max(a[0],a[1]);
        
        for(int i = 2; i < n; i++){
            dp[i] = max(a[i] + dp[i-2], dp[i-1]);    
        }
        
        return dp[n-1];
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.