Cut the rope problem (dynamic programming, greedy, recursion)

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 rope is referred to as k [0], k [1 ] , ..., k [m]. Will k [0] xk [1] x ... xk [m] maximum possible product is how much?
For example, when the length of the rope is 8:00, we cut it into lengths of three sections 2,3,3 of the product obtained at this time is the maximum 18.

Thinking

Note that
when the length is greater than 3 f [n] to obtain maximum product rope

Dynamic Programming

Feature
from the analysis of the problem down to solve the problem from the bottom up;

  • Seeking an optimal solution; (maximum or minimum)
  • Problems can be decomposed into several sub-problems, and smaller sub-problems as well as between overlapping subproblems
  • Small problems after decomposition there is an optimal solution, if the optimal solution of the whole problem of the optimal solution of small problems can be combined, you can use dynamic programming
achieve
public int cutRope(int target) {
    //排除特殊情况
    if (target < 2) {
        return 0;
    }
    if (target == 2) {
        return 1;
    }
    if (target == 3) {
        return 2;
    }
    int[] products = new int[target + 1];
    products[0] = 0;
    products[1] = 1;
    products[2] = 2;
    products[3] = 3;
    for (int i = 4; i <= target; i++) {
        int max = 0;
        for (int j = 1; j <= i / 2; j++) {
            int product = products[j] * products[i - j];
            max = Math.max(max, product);
        }
        products[i] = max;
    }
    return products[target];
}

greedy

  • Since multiplication, then in addition to a majority of the multiplication, the result will be.
  • Therefore, from 2 to start thinking. But the words are divided into 2 inevitably contain an odd number of segments 1, because 1 equivalent of a waste of a multiplier, so if the last one left when we should be 3. So he goes to get into a number of segments of length 2, 3 it's the best.
  • And because 2 * 2 * 2 <3 * 3 Description 3 2 2 3 not as good as, and should make relatively more than 2 to 3.
  • The number of three so let division, if the remainder is 2, is divided into a 2, N 3-optimal solution, if the remainder is 1, it should be divided into 2 2, N-1 is the optimal 3 solution
achieve
public int cutRope(int target) {
    //排除特殊情况
    if (target < 2) {
        return 0;
    }
    if (target == 2) {
        return 1;
    }
    if (target == 3) {
        return 2;
    }
    int timesOf3 = target / 3;
    if (target - timesOf3 * 3 == 1) {
        timesOf3--;
    }
    int timesOf2 = (target - timesOf3 * 3) / 2;
    int result = (int) (Math.pow(3, timesOf3) * Math.pow(2, timesOf2));
    return result;
}

Recursion

Although dynamic programming than the recursive high I do not know to go there, because there are a lot of repeated recursively solving the case
, but I see on the Internet, it does not seem to cut the rope recursive solution, so when you see a play ...

Thinking

f(n)=max(f(i)*f(n-i)) 0<i<n

achieve
public int cutRope03(int target) {
    if (target < 2) {
        return 0;
    }
    if (target == 2) {
        return 1;
    }
    if (target == 3) {
        return 2;
    }
    int max = 0;
    for (int i = 1; i <= target/2; i++) {
        max = Math.max(cutRope03Core(i) * cutRope03Core(target - i), max);
    }
    return max;
}

private int cutRope03Core(int target) {
    if (target < 4) {
        return target;
    }
    int max = 0;
    for (int i = 1; i <= target/2; i++) {
        max = Math.max(cutRope03Core(i) * cutRope03Core(target - i), max);
    }
    return max;
}

Guess you like

Origin www.cnblogs.com/aiguozou/p/11576036.html