My simple O(n) Java solution

  • 0
    public class Solution {
        public int maxSubArray(int[] nums) {
            if(nums.length==0) return Integer.MIN_VALUE;
            int min = nums[0];
            int result = nums[0];
            for(int i=1;i<nums.length;i++){
                if(nums[i]>result) result = nums[i];
                if(nums[i]-min>result) result = nums[i]-min;
                if(nums[i]<min) min = nums[i];
            return result;

Log in to reply

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