5-line C simple solution. 1ms, O(1)space, O(N)time.


  • 2
    T
    int rob(int a[], int n) {
        if (n == 0) return 0;
        if (n > 1 && a[0] > a[1]) a[1] = a[0];
        for (int i = 2; i < n; i++)
            a[i] = a[i]+a[i-2] > a[i-1] ? a[i]+a[i-2] : a[i-1];
        return a[n - 1];
    }

Log in to reply
 

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