class Solution { public int maxSubArray(int[] nums) { int sum=0; int max=0; for(int i=0;i<nums.length;i++){ sum+=nums[i]; if(sum<0){ sum=0; } else{ if(sum>max){ max=sum; } } } if(max==0) max=Integer.MIN_VALUE; for(int i=0;i<nums.length;i++){ if(nums[i]>max) max=nums[i]; } return max; } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter