Java solution O(n)


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

Log in to reply
 

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