less than 10 line Java O(n) time complex. (1ms)


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

Log in to reply
 

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