Leetcode 410. Split Array Largest Sum

Source: Internet
Author: User
Tags min split valid
Given an array which consists of non-negative integers and a integer m, you can split the array into m non-empty continuo US subarrays. Write a algorithm to minimize the largest sum among these m subarrays.

Note:
If n is the length of an array, assume the following constraints is satisfied:

1≤n≤1000
1≤m≤min (50 , N)
E

This problem has two solutions, dynamic planning and binary search, dynamic planning requires three-layer cycle, not as efficient as two-point search

DP solution. This was obviously not as good as the binary search solutions;

But it did pass OJ.

DP[S,J] is the solution for splitting Subarray n[j]...n[l-1] into s parts.  Dp[s+1,i] = min{max (dp[s,j], n[i]+...+n[j-1])}, i+1 <= J <= L-s This solution does don't take advantage of the fact That the numbers is non-negative (except to break the inner loop early). That's a loss. 
(On the other hand, it can is used for the problem containing arbitrary numbers) public int splitarray (int[] nums, int m)
    {int L = nums.length;
    int[] S = new int[l+1];
    s[0]=0;

    for (int i=0; i<l; i++) s[i+1] = s[i]+nums[i];
    Int[] dp = new INT[L];

    for (int i=0; i<l; i++) dp[i] = s[l]-s[i];
            for (int s=1, s<m; s++) {for (int i=0; i<l-s; i++) {dp[i]=integer.max_value;
                for (int j=i+1; j<=l-s; J + +) {int t = Math.max (Dp[j], s[j]-s[i]);
                  if (T<=dp[i])  dp[i]=t;
            else break;
}}} return dp[0]; }

Using binary search

public class Solution {public
    int splitarray (int[] nums, int m) {
        int max = 0; Long sum = 0;
        for (int num:nums) {
            max = Math.max (num, max);
            sum + = num;
        }
        if (m = = 1) return (int) sum;
        Binary search
        long l = max; long r = sum;
        while (L <= R) {
            Long mid = (L + r)/2;
            if (valid (Mid, Nums, m)) {
                r = mid-1;
            } else {
                L = mid + 1;
            }
        }
        return (int) l;
    }
    Public boolean valid (long target, int[] nums, int m) {
        int count = 1;
        Long total = 0;
        for (int. num:nums) {Total
            + = num;
            if (Total > Target) {total
                = num;
                count++;
                if (Count > m) {
                    return false;
                }
            }
        }
        return true;
    }
}

The

Vaild function is a greedy way of dividing an array into multiple parts, each time accumulating until it exceeds target. If the score is greater than M, the target is too small, should be increased, in the binary search correction L = mid +1; if it is less than M, the target selection is too large, and the correct R = mid-1. If equal to M, the installation of greed is a way to find out whether or not to be smaller, so the correction R = mid-1.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.