Simple O(n) C# solution


  • 0
    C

    Simple solution in C#:

    public int MaxProfit(int[] p)
    {
        if (p == null || p.Length < 2) return 0;
        int min = p[0], max = p[0], preGain = 0;
        for (int i = 1; i < p.Length; i++)
            if (p[i] < min)
            {
                if (max - min > preGain)
                    preGain = max - min;
                min = p[i];
                max = p[i];
            }
            else if (p[i] > max)
                max = p[i];
        return preGain > max - min ? preGain : max - min;
    }
    

Log in to reply
 

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