My C++ solution using DP


  • 0
    H
    public:
        int rob(vector<int>& nums) {
            int max = 0;
            vector<int> S;
            S.resize(nums.size());
            
            for(int i = 0; i < nums.size(); i++){
                S[i] = nums[i];
            }
            
            for(int i = 0; i < nums.size(); i++){
                for(int j = 0; j < i; j++){
                    if(j + 1 != i && nums[i] + S[j] > S[i]){
                        S[i] = nums[i] + S[j]; if(S[i] > max) max = S[i];}
                }
            }
            
            return max;
        }
    };

Log in to reply
 

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