answer:
For this problem, cutting a length of rope can be solved by the recursive method, but when used in recursive solution will produce a series of repeats,
such as computing f (6), can be divided into f (2), f (4 ); Calculation f (8), can be divided into f (4), f (4 ); generates a repetitive calculating f (4); a recursive top-down; we can use a bottom-up is calculated (dynamic programming) in this problem reduce double counting, and can quickly calculate the greedy algorithm.
Method One: dynamic programming
From the meaning of problems n> 1, m> 1; thus a minimum of 2 n, and the meaning of the questions, cut least once, and cut into 1 1 2; i.e., when n = 2, returns 1; bottom-up dynamic programming Therefore we need to set the initial value dp [0] = 0, dp [1] = 1, dp [2] = 1; was then calculated from the 3 = n, the n-up; in the calculation process, we use two for loop to cut implementation process;
Transfer equation is max (dp [i], max (j * (ij), j * dp [ij]))
code show as below:
class Solution:
def cuttingRope(self, n):
if n == 2:
return 1
dp = [0] * (n+1)
dp[0] = 0
dp[1] = 1
dp[2] = 1
for i in range(3,(n + 1)):
# max1 = 0
for j in range(1,(i//2)+1):
dp[i] = max(dp[i], max(j*(i-j), j * dp[i - j]))
return dp[n]
As can be seen from the above code we use two for loops,
the outer for-loop calculate the length of the rope from the most 3 to n of the product;
the inner for-loop i rope during the cutting, cutting to obtain a product of maximum rope i value; range wherein j is 1, (i // 2) + 1; because if the length is 5, and then cut into 3 and 2 and 3 are the same effect;
State transition equation meanings:
max (dp [i], max (j * (ij), j * dp [ij]))
We want to remove the rope length i is the maximum product after cutting, wherein the maximum product is initialized to 0, j 1 changes from the start that our cutting knife, the knife first process of change; then i becomes j and ij, then we need to consider the following cases, namely whether to continue cutting the rope; because we have become a two-stage cut, so there are four possible;
- j no longer cut, ij no longer cut
- Then cut j, ij no longer cut
- Then cut j, ij then cut
- j no longer cut, ij then cut
According to the analysis, we can then remove cropped both cases j (2,3), only the remaining first and fourth. The following reasons:
for example, the case where 11 i:
j is 5, ij is 6;
wherein j varying from 1 to 5;
when 3, ij when j is 8
. 3 8
. 3 no cutting
8 can be cleaved as follows:
1 , 4
2,6
3,5
4,4
us look 3,2,6 this set of data;
2 no longer cut, cut to 6;
i.e., the maximum product becomes. 3 2 DP [6]
which is j is 5, ij is a sub-process 6;
Therefore, since j from 1 starts to change, so that the cutting result value j is increased during storage in less than j, and therefore, no cut j.
Therefore, the state transition equation is as follows:
max (DP [I], max (J * (ij of), DP * J [ij of]))
Method Two: greedy algorithm:
refer to the Great God of practice:
https://leetcode-cn.com/problems/jian-sheng-zi-lcof/solution/mian-shi-ti-14-i-jian-sheng-zi- tan-xin-si-xiang- by /
The idea is to get 3 more cut, the greater the product.
If the remainder is divided by 31, the last one is 4, is cut into 2 * 2;
if the remainder is divided by 32, the last one is a 2
code is as follows:
class Solution:
def cuttingRope(self, n):
if n == 2:
return 1
if n == 3:
return 2
if n % 3 == 0:
return 3**(n//3)
if n % 3 == 1:
return 3**(n//3 - 1) * 4
if n % 3 == 2:
return 3**(n//3) * 2
Attached Dynamic Programming code for the four cases:
the transfer equation:
max (DP [I], max (J * (ij of), DP [J] * (ij of), DP * J [ij of], DP [J ] * dp [i - j] ))
class Solution:
def cuttingRope(self, n):
if n == 1:
return 1
if n == 2:
return 1
dp = [0] * (n+1)
dp[0] = 0
dp[1] = 1
dp[2] = 1
for i in range(3,(n + 1)):
# max1 = 0
for j in range(1,(i//2)+1):
dp[i] = max(dp[i], max(j*(i-j), dp[j] * (i-j), j * dp[i - j], dp[j] * dp[i - j]))
return dp[n]