Dynamic programming and greedy algorithms _ cut the rope question

problem:

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] maximum possible product is how much? 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.

Solving:

1. Dynamic Programming
seeking an optimal solutions (maximum or minimum), and that the problem can be decomposed into several sub-problems, and there is overlap between the sub-problems of smaller sub-problems, consider dynamic programming
(1 ) can analyze whether large problem into smaller problems
(2) there is an optimal solution for each small problem after the decomposition
3) optimal solution (small problems combined to get the best solution of the whole problem

2. greedy algorithm:
Each step can make a greedy choice, based on this selection, can determine the optimal solution
1. When greedy algorithm to solve the problem, not to be considered as a whole the best, he made is the optimal solution in the sense of the local
2. Select the greedy strategy must have no after-effect, namely, a state before the process will not affect the state's future, only the current state of the related
3. Title greedy strategy: When n> = 5, as much as possible of the cut length of the rope 3; when the remaining length of the rope 4, the two lengths of rope cut the rope 2

 

1. First recursive dynamic programming, dynamic programming approach is actually a time-efficient district with space efficiency change:

the Test class {public 
public static int getMax (n-int) {
     // desired length n> 1, where it returns 0 indicates an invalid entry

IF (n-<2) {
return 0;
}
     // the length of 2, the number of required segments cut m> 1, it is the largest. 1 = 1x1-

IF (n-== 2) {
  return. 1;
}
     // length is 3, the number of segments required cut m> 1, it is at most 2 = 1x2

IF (== n-3) {
return 2;
}
// store results from the maximum length of the 0-n
int [] = result new new int [n-+. 1];
result [0] = 0;
result [. 1] =. 1;
result [2] = 2;
Result [. 3] =. 3;
// begin the solution from the bottom up
int max = 0;
for (int I =. 4; I <= n-; I ++) {
       // each product the maximum empty

0 = max;
       // to calculate because F (j) is multiplied by the maximum value of f (ij), and j is repeated more than half of i is calculated, it is only necessary to consider i j is not more than half of the case

for (int j = . 1; J <= I / 2; J ++) {
         // calculate f (j) is multiplied by F (ij of)

int = tempResult Result [J] * Result [ij of];
IF (max <tempResult) {
max = tempResult;
}
         // update the table F (I) = max (F (J) · F (ij of))

Result [I] = max;
}
}
max = Result [n-];
return max;
}

}

2. obtained using the greedy algorithm Optimal solution:
public static int getMaxValue(int n){
if(n<2) {
return 0;
}
if(n==2) {
return 1;
}
if(n==3) {
return 2;
}
//先求出长度为3的段一共有多少段
int countOfThree=n/3;
//再看除了长度为3的段,还剩下多少米,如果还剩1米则表示有(n-1)个3和一个4,
// 所以要把上一步算出来的3的个数减1,把最后的4剪成两段长度为2的绳子
if(n-countOfThree*3==1){
countOfThree-=1;
}
//当剩下的绳子长度为4时,把绳子剪成两段长度为2的绳子
int countOfTwo=(n-countOfThree*3)/2;
//计算所有的3的countOfThree次方,再乘以剩下2的countOfTwo次方
return (int) ((Math.pow(3, countOfThree))*(Math.pow(2, countOfTwo)));
}
 

Guess you like

Origin www.cnblogs.com/cdlyy/p/12153369.html