DP C# solution beats 95%


  • 1
    L
    public class Solution {
        public int MaxProduct(int[] nums) {
            var result = 0;
                if (nums != null && nums.Length > 0)
                {
                    result = nums[0];
                    var maxn_1 = result;
                    var minn_1 = result;
                    for (int i = 1; i < nums.Length; i++)
                    {
                        var max = maxn_1 * nums[i];
                        var min = minn_1 * nums[i];
                        maxn_1 = Math.Max(nums[i], Math.Max(max, min));
                        minn_1 = Math.Min(nums[i], Math.Min(max, min));
                        result = Math.Max(result, maxn_1);
                    }
                }
                return result;
        }
    }
    

Log in to reply
 

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