Java O(n) time O(1) space simple solution (accepted)


  • 0
    P
        public int maxSubArray(int[] nums) {
            if (nums == null || nums.length == 0)
                return 0;
            int curMax = 0;
            int maxMax = Integer.MIN_VALUE;
            for (int i = 0; i < nums.length; i++) {
                curMax += nums[i];
                maxMax = Math.max(maxMax, curMax);
                if (curMax < 0) {
                    curMax = 0;
                }             
            }
            return maxMax;
        }
    

Log in to reply
 

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