C code by DP, with 7 lines and 0ms.


  • 0
    W
    int rob(int* nums, int numsSize) {
        int pre = 0, cur = 0;
        for (int i = 0; i < numsSize; ++i) {
            int tmp = nums[i] + pre;
            pre = cur;
            cur = (tmp > pre ? tmp : pre);
        }
        return cur;
    }

Log in to reply
 

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