Share my c codes


  • 0
    K

    #define max(x, y) (x > y) ? x : y
    int rob(int* nums, int numsSize) {
    if (numsSize < 2)
    return nums[0];

    int prev = nums[0], cur = max(nums[1], nums[0]);
    
    for (int i = 2; i < numsSize; i++) {
        int temp = cur;
    
        cur = max(cur, prev + nums[i]);
        prev = temp;
    }
    return cur;
    

    }


Log in to reply
 

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