0ms C++ Solution


  • 0
    P
    class Solution {
    public:
        int rob(vector<int>& nums) {
        int n = nums.size();
        if(n==0)	return 0;
        if(n==1)	return nums[0];
    	int	bag[n+1];
    	int record;
    	bag[0] = 0;
    	bag[1] = nums[0];
    	for(int i=2; i<n; i++)
    		bag[i]= (bag[i-2]+nums[i-1])>bag[i-1]?(bag[i-2]+nums[i-1]):bag[i-1];
    	record = bag[n-1];
    	bag[1] = 0;
    	for(int i=2; i<n+1; i++)
    		bag[i]= (bag[i-2]+nums[i-1])>bag[i-1]?(bag[i-2]+nums[i-1]):bag[i-1];
    	return (bag[n]>record)?bag[n]:record;
        }
    };

Log in to reply
 

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