3ms to solve this problem


  • 0
        int length = num.size();
        if(length==0)
            return 0;
        if(length==1)
            return num[0];
        int g[length] = {0};
        int l[length] = {0};
        l[0] = g[0] = num[0];
        l[1] = num[1];
        g[1] = max(l[0],l[1]);
        for(int i=2;i<length;i++)
        {
            l[i] = num[i]+g[i-2];
            g[i] = max(g[i-1],l[i]);
        }
        return g[length-1];

Log in to reply
 

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