Painter problem (Dynamic programming) __ Dynamic programming

Source: Internet
Author: User

Import java.util.*;

        Painter problem public class painterproblem{//(1) Get the minimum time required to complete all artwork (dynamic programming method) public static int getTime (Int[]arr,int num) { if (arr==null| | arr.length==0| |
        num<1) {throw new RuntimeException ("Input parameter Error!");
        int mintime=0; Only one person's case if (num==1) {for (int i=0;i<arr.length;i++) {Mintime
            +=arr[i];
        return mintime; 
        } int[]sumarr=new Int[arr.length];   Int[]map=new Int[arr.length];
        Generating a dynamic programming array of length n sumarr[0]=arr[0];
        MAP[0]=ARR[0];
              for (int i=1;i<sumarr.length;i++) {sumarr[i]=sumarr[i-1]+arr[i];
        Map[i]=sumarr[i]; for (int i=1;i<num;i++) {for (int j=map.length-1;j>i-1;j--) {i
                   NT Min=integer.max_value; for (int k=i-1;k<j;k++) {int Cur=matH.max (Map[k],sumarr[j]-sumarr[k]);
                   Min=math.min (min,cur);
             } map[j]=min;
    } return map[arr.length-1]; }//(2) optimized dynamic planning public static int Solution2 (int[] arr, int num) {if (arr = null | | arr.length = 0 | | nu
        M < 1) {throw new RuntimeException ("err");
        } int[] Sumarr = new Int[arr.length];
        int[] map = new Int[arr.length];
        Sumarr[0] = arr[0];
        Map[0] = arr[0];
            for (int i = 1; i < sumarr.length i++) {sumarr[i] = Sumarr[i-1] + arr[i];
        Map[i] = Sumarr[i];
        } int[] Cands = new Int[arr.length]; for (int i = 1; i < Num. i++) {for (int j = map.length-1 J > i-1; j--) {int Minpa
                r = Cands[j]; int Maxpar = j = = Map.length-1?
                J:cands[j + 1];
                int min = Integer.max_value; for (int k = mInpar; K < Maxpar + 1;
                    k++) {int cur = Math.max (map[k], sumarr[j]-sumarr[k]);
                        if (cur <= min) {min = cur;
                    CANDS[J] = k;
            } map[j] = min;
    } return map[arr.length-1]; }//(3) The optimal solution public static int Solution3 (int[] arr, int num) {if (arr = null | | arr.length = 0 | | | Lt
        1) {throw new RuntimeException ("err");
            } if (Arr.length < num) {int max = Integer.min_value;
            for (int i = 0; I!= arr.length i++) {max = Math.max (max, arr[i]);
        return to Max;
            else {int minsum = 0;
            int maxsum = 0;
            for (int i = 0; i < arr.length i++) {maxsum + = Arr[i]; while (Minsum!= maxSum-1) {int mid = (minsUm + maxsum)/2;
                if (Getneednum (arr, mid) > num) {minsum = mid;
                else {maxsum = mid;
        } return maxsum;
        } public static int getneednum (int[] arr, int lim) {int res = 1;
        int stepsum = 0;
            for (int i = 0; I!= arr.length. i++) {if (Arr[i] > Lim) {return integer.max_value;
            } Stepsum + = Arr[i];
                if (Stepsum > Lim) {res++;
            Stepsum = Arr[i];
    } return res;
        }//Get random array public static int[] Generaterandomarray (int length) {int[] result = new Int[length];
        for (int i = 0; I!= result.length i++) {result[i] = (int) (Math.random () * 10) + 1;
    return result; }//print array public static void PrintArray (int[] arr) {for (int i = 0; I!= aRr.length;
        i++) {System.out.print (Arr[i] + "");
    } System.out.println ();
       public static void Main (String[]args) {//system.out.println ("Hello"); int[]arr2={1,1,3,3,4};        Total number of works int num=2;

        The total number of painter System.out.println (GetTime (arr2,num));
        int[] arr = Generaterandomarray (300);
        int[]arr={1,1,3,3,4};
        int painternum = 2;
        System.out.println (Solution2 (arr, painternum));

        System.out.println (Solution3 (arr, painternum));
        arr = Generaterandomarray (1000000);
        Painternum = 10000;
        Long start = System.currenttimemillis ();
        System.out.println (Solution3 (arr, painternum));
        Long end = System.currenttimemillis ();
    System.out.println ("Cost Time:" + (End-start) + "MS"); }
}

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.