C# Accepted solution using DP


  • 0
    J
    public int MaxSubArray(int[] nums) 
        {
            if (nums.Length == 0) return 0;
            if (nums.Length == 1) return nums[0];
            
            var array = new int[nums.Length];
            array[0] = nums[0];
            var max = array[0];
            for (int i = 1; i < nums.Length; i++)
            {
                if (array[i-1] < 0) 
                {
                    array[i] = nums[i];
                }
                else 
                {
                    array[i] = array[i-1] + nums[i];
                }
                max = Math.Max(max, array[i]);
            }
            return max;
        }
    

Log in to reply
 

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