DP c++ concise solution


  • 0
    M
    class Solution {
    public:
        int rob(vector<int>& nums) {
            int f1, f2, i, temp;
            f1 = 0;
            if(nums.size()){
                f2 = nums[0];
                f1 = (nums.size() > 1 && nums[0] < nums[1])? nums[1] : nums[0];
                for(i = 2; i < nums.size(); i++){
                    temp = f1;
                    f1 = (nums[i] + f2) > f1? (nums[i] + f2) : f1;
                    f2 = temp;
                }
            }
            return f1;
        }
    };

Log in to reply
 

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