O(n) with O(1) extra space


  • 0
    M
    public class Solution {
        public int maxSubArray(int[] nums) {
            if(nums==null || nums.length==0){
                return 0;
            }
            int max=Integer.MIN_VALUE;
            int sum=0,i=0;
            while(i<nums.length){
                sum+=nums[i];
                if(sum>max){
                        max=sum;
                    }
                if(sum<=0){
                    sum=0;
                    
                }
                i++;
            }
            return max;
            
        }
    }
    

Log in to reply
 

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