Offer] [[14]] [cut the rope

Title Description

  You give a string of length n, m please cut the rope section (m, n are integers, n> 1 and m≥1). The length of each segment is referred to as a rope k[0]、k[1]、……、k[m]. k[0]* k[1]*…*k[m]What is the maximum possible product? For example, when the rope length is 8, we cut it into lengths of the three sections 2,3,3, 18 at this time to obtain the maximum product.

Ideas analysis

  1. The product of the maximum length of each segment of the defined function f (n) n is a length of rope cut into several sections: dynamic programming

    • f(0) = 0
    • f(1) = 0 Here because the length of the rope is greater than the value of the product, so when the actual operation using the length of the rope
    • f(2) = 1 Ditto
    • f(3) = 2 Ditto
    • f(n) = max(f(i)*f(n-i)), 1< i <n

      Thus calculated in order from bottom to top

  2. Greedy algorithm: as much as possible to cut the length of the rope segment 3

Test Case

  1. Function Test (length greater than 5)
  2. Boundary value test (1,2,3,4 length)

Java code

public class Offer14 {
    public static void main(String[] args) {
        System.out.println("功能测试--->");
        test1();
        System.out.println("边界值测试--->");
        test2();
    }
    
    public static int maxProductAfterCutting(int n) {
        return Solution2(n);
    }

    /**
     * 解法一:动态规划
     * 思路: 定义函数f(n) 为将长度为n的绳子剪成若干段后各段长度乘积的最大值
     * 1.  f(0) = 0
     * 2.  f(1) = 0  因为此处绳子的长度大于乘积的值, 所以实际操作的时候 使用绳子的长度
     * 3.  f(2) = 1  同上
     * 4.  f(3) = 2  同上
     * 5.  f(n) = max(f(i)*f(n-i)), 1< i <n
     * 
     * @param n
     * @return
     */
    private static int Solution1(int length) {
        
        if(length<=1) {
            return 0;
        }
        if(length == 2 ) {
            return 1;
        }
        if(length == 3) {
            return 2;
        }
        int[] product = new int[length+1];
        product[0] = 0;
        product[1] = 1;
        product[2] = 2;
        product[3] = 3;
        
        for(int i=4 ;i<=length;i++) {
            int max = 0;
            for(int j=1;j<=i/2;j++) {
                if(max < product[j]*product[i-j]) {
                    max = product[j] * product[i-j];
                }
            }
            product[i] = max;
        }
        
        return product[length];
    }
    
    /**
     * 解法二: 贪心算法
     * 思路: 尽可能多的减成长度为3的绳子段
     */
    private static int Solution2(int length) {
        if(length<=1) {
            return 0;
        }
        if(length==2) {
            return 1;
        }
        if(length==3 ) {
            return 2;
        }
        
        int timeOf3 = length/3;
        if(length - timeOf3*3 == 1) {
            timeOf3 -= 1;
        }
        int timeOf2 = (length-timeOf3*3) / 2;
        return (int) Math.pow(3, timeOf3) * (int)Math.pow(2,timeOf2);
    }
    
    /**
     * 测试功能
     */
    private static void test1() {
        System.out.println("绳子长度为8:"+maxProductAfterCutting(8));
    }
    /**
     * 测试边界值
     */
    private static  void test2() {
        System.out.println("绳子长度为0:"+maxProductAfterCutting(0));
        System.out.println("绳子长度为1:"+maxProductAfterCutting(1));
        System.out.println("绳子长度为2:"+maxProductAfterCutting(2));
        System.out.println("绳子长度为3:"+maxProductAfterCutting(3));
        System.out.println("绳子长度为4:"+maxProductAfterCutting(3));
    }
}

Code link

Offer to prove safety codes -Java

Guess you like

Origin www.cnblogs.com/haoworld/p/offer14-jian-sheng-zi.html