A C++ solution runtime:0ms


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

Log in to reply
 

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