leetcode maximum suborder and easy

topic link

topic link

The practice of log(n) is provided here.

First, find the largest subsequence sum on the left, then find the largest subsequence sum on the right, and then find the middle to combine.

class Solution {
    public static int maxSubArray(int[] nums) {
        if(nums.length == 0) return 0;
        return solve(nums, 0, nums.length - 1);
    }
    public static int solve(int[] nums, int l, int r) {
    	if(l > r) return Integer.MIN_VALUE;
    	if(l == r) return nums[l];
    	int mid = l + r >> 1;
    	int L = solve(nums, l, mid - 1);
    	int R = solve(nums, mid + 1, r);
    	int sum = nums[mid] , t = nums[mid];
    	for(int i = mid - 1; i >= l; i--) {
    		t += nums[i];
    		sum = Math.max(sum, t);
    	}
    	t = sum;
    	for(int i = mid + 1; i <= r; i++) {
    		t += nums[i];
    		sum = Math.max(sum, t);
    	}
    	return Math.max(Math.max(L, R), sum);
    }
}

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326640232&siteId=291194637