My answer:
public class Solution { public int maxSubArray(int[] nums) { int globalMax = nums[0]; int localMax = nums[0]; for (int i = 1; i < nums.length; i ++) { // localMax is the bigger value which includes "i"th number, so that the globalMax can be the result of contiguous subarray localMax = Math.max(nums[i], localMax + nums[i]); globalMax = Math.max(globalMax, localMax); } return globalMax; } }
No comments:
Post a Comment