Accepted O(n) Java solution


  • 0
    S
    public class Solution {
        public int maxSubArray(int[] nums) {
            int n = nums.length;
            int[] mem = new int[n];
            int max = nums[0];
            
            mem[0] = nums[0];
            
            for (int i = 1; i < n; i++){
                if(nums[i] > nums[i] + mem[i-1]) mem[i] = nums[i];
                else mem[i] = nums[i] + mem[i-1];
                
                if(max < mem[i]) max = mem[i];
            }
            
            return max;
        }
    }
    

Log in to reply
 

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