Simple Java O(n) solution.


  • 1
    V
     public int maxSubArray(int[] nums) {
        int max_so_far = nums[0], sum = nums[0];
        for(int i=1; i<nums.length; i++){
            sum = (nums[i] > sum+nums[i]) ? nums[i] : sum+nums[i];
            if(sum > max_so_far) max_so_far = sum;
        }
        return max_so_far;
    }

Log in to reply
 

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