Java solution for Maximum Subarray


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

Log in to reply
 

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