3 line java solution


  • 0
    W
    public int maxProfit(int[] p) {
        int t = 0, s = 0, l = p.length, i = 0;
        while (++i < l) if (t + (s = p[i] - p[i-1]) > t) t += s;
        return t;
    }
    

    buy low sell high


Log in to reply
 

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