53. Maximum Subarray

527 ワード

class Solution {
    public int maxSubArray(int[] nums) {
        int cur = nums[0];
        int max = nums[0];
        
        for (int i = 1; i < nums.length; i++) {
            cur = Math.max(nums[i], cur + nums[i]);
            max = Math.max(cur, max);
        }
        
        return max;
    }
}
Runtime: 0 ms, faster than 100.00% of Java online submissions for Maximum Subarray.
Memory Usage: 39 MB, less than 50.44% of Java online submissions for Maximum Subarray.