Simple C solution easy to understand


  • 0
    E

    Calculate every Rob/NoRob max number until the last one.

    int rob(int* nums, int numsSize) {
        int maxRob = 0;
        int maxNoRob = 0;
        for (int i = 0; i < numsSize; i++) {
            int curRob = maxNoRob + nums[i];
            int curNoRob = maxRob;
            maxNoRob = curNoRob > maxNoRob ? curNoRob : maxNoRob;
            maxRob = curRob > maxRob ? curRob : maxRob;
            //printf("%d %d\n", maxNoRob, maxRob);
        }
        return maxRob > maxNoRob ? maxRob : maxNoRob;
    }
    

Log in to reply
 

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