Simple solution, beats 89%


  • 0
    public int MaximumProduct(int[] nums)
    {
        int[] MinMax = new int[5];
        MinMax[0] = int.MaxValue;
        MinMax[1] = int.MaxValue;
        MinMax[2] = int.MinValue;
        MinMax[3] = int.MinValue;
        MinMax[4] = int.MinValue;
    
        for (int i = 0; i < nums.Length; i++)
        {
            int n = nums[i];
            //comparing with big numbers
            if (n >= MinMax[4]) //greater than all
            {
                MinMax[2] = MinMax[3];
                MinMax[3] = MinMax[4];
                MinMax[4] = n;
            }
            else if (n >= MinMax[3])
            {
                MinMax[2] = MinMax[3];
                MinMax[3] = n;
            }
            else if (n >= MinMax[2])
            {
                MinMax[2] = n;
            }
            //comparing with small numbers
            if (n <= MinMax[0]) //smaller than all
            {
                MinMax[1] = MinMax[0];
                MinMax[0] = n;
            }
            else if (n <= MinMax[1])
            {
                MinMax[1] = n;
            }
        }
    
        return Math.Max(MinMax[0] * MinMax[1] * MinMax[4], MinMax[4] * MinMax[3] * MinMax[2]);
    }
    

Log in to reply
 

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