C++ 0ms DP


  • 0
    int rob(vector<int>& nums) {
            if(nums.size()==0) return 0;
            //m[i]=max{m[i-1],m[i-2]+nums[i]}
            vector<int>m(nums.size());
            m[0]=nums[0];
            m[1]=max(m[0],nums[1]);
            for(int i=2;i<nums.size();i++){
                m[i]=max(m[i-1],m[i-2]+nums[i]);
            }
            return m[nums.size()-1];
        }
    

Log in to reply
 

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