package leetcode;

/**
 * https://leetcode.com/problems/capacity-to-ship-packages-within-d-days/
 */
public class Problem1014 {
    public int shipWithinDays(int[] weights, int D) {
        // TODO
        return 0;
    }

    public static void main(String[] args) {
        Problem1014 prob = new Problem1014();
        System.out.println(prob.shipWithinDays(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 5)); // 15
//        System.out.println(prob.shipWithinDays(new int[]{1, 2, 3, 10, 5, 6, 7, 8, 9}, 5)); // 15
//        System.out.println(prob.shipWithinDays(new int[]{1, 2, 10, 3, 5, 6, 7, 8, 9}, 5)); // 13
//        System.out.println(prob.shipWithinDays(new int[]{3, 2, 2, 4, 1, 4}, 3)); // 6
//        System.out.println(prob.shipWithinDays(new int[]{1, 2, 3, 1, 1}, 4)); // 3
    }
}

@@ -187,38 +187,49 @@
     
-// TODO%0A        return
+int left = 0;%0A        int right =
  0;%0A    
 %7D%0A%0A 
@@ -228,572 +228,772 @@
     
-%7D%0A%0A
     
-public static void main(String%5B%5D args) %7B%0A        Problem1014 prob = new Problem1014();%0A        System.out.println(prob.shipWithinDays(new int%5B%5D%7B1, 2, 3, 4, 5, 6, 7, 8, 9, 10%7D, 5)); // 15%0A//        System.out.println(prob.shipWithinDays(new int%5B%5D%7B1, 2, 3, 10, 5, 6, 7, 8, 9%7D, 5)); // 15%0A//        System.out.println(prob.shipWithinDays(new int%5B%5D%7B1, 2, 10, 3, 5, 6, 7, 8, 9%7D, 5)); // 13%0A//        System.out.println(prob.shipWithinDays(new int%5B%5D%7B3, 2, 2, 4, 1, 4%7D, 3)); // 6%0A//        System.out.println(prob.shipWithinDays(new int%5B%5D%7B1, 2, 3, 1, 1%7D, 4)); // 3
+for (int w : weights) %7B%0A            left = Math.max(left, w); // max%0A            right += w; // sum%0A        %7D%0A        while (left %3C right) %7B%0A            int mid = (left + right) / 2;%0A            if (isPossible(weights, D, mid)) %7B%0A                right = mid;%0A            %7D else %7B%0A                left = mid + 1;%0A            %7D%0A        %7D%0A        return left;%0A    %7D%0A%0A    private static boolean isPossible(int%5B%5D weights, int d, int max) %7B%0A        int sum = 0;%0A        int split = 1;%0A        for (int weight : weights) %7B%0A            if (sum + weight %3E max) %7B%0A                sum = 0;%0A                split++;%0A                if (split %3E d) %7B%0A                    return false;%0A                %7D%0A            %7D%0A            sum += weight;%0A        %7D%0A        return true;
 %0A   

Solve problem 1014