My O(1) space DP Java solution


  • 0
    W

    I use Kadane algorigthm, more details in my blog,
    blog click here

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

Log in to reply
 

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