Recursive traversal and for loop traversal: recursive traversal implementation, easy to understand

1. Source of ideas: Force button question 14- I. Cut the rope
to give you a rope of length n, please cut the rope into m segments of integer length (m and n are integers, n>1 and m>1) , The length of each rope is recorded as k[0],k[1]...k[m-1]. Will the k[0] x k[1] x ... x k[m-1]maximum possible product is how much? For example, when the length of the rope is 8, we cut it into three pieces with lengths of 2, 3, and 3. The maximum product obtained at this time is 18.

示例 1:
输入: 2
输出: 1
解释: 2 = 1 + 1, 1 × 1 = 1

示例 2:
输入: 10
输出: 36
解释: 10 = 3 + 3 + 4, 3 × 3 × 4 = 36

2. Thinking:
We usually think of dynamic programming solution when we do too many questions, expand the length of the rope from the bottom up, and complete the problem solution. But beginners may only think ofBrute force search, traverse all rope cutting methods, compare and get results
For traversal, we usually think of for loop traversal, but for this problem, it is difficult to think of what each for loop represents and what its range is, so the for loop solution fails. However, the recursive traversal method is simpler to implement and understand, so you should remember that it is more commonly used when solving problems .
Ideas Source: Author: z1m, Source: buckle force
method: recursive traversal violence
we tend to form in the minds of a very violent intuitive solution is to enumerate all cases, find the product of the maximum of the solution.
Set F (n) of length n, the maximum product of the rope can be obtained, for each F (n), can be obtained by decomposition:
Insert picture description here
see that we can solve the figure from F(n)the problem into solving F(n-1)the problems, and so on until solving to F(2)when F(2) = 1, recursive back, the problem has been resolved.This is the idea of ​​divide and conquer.
The solution to the divide and conquer idea is often recursion. Note that every time we cut a piece of rope into two pieces, the remaining part can be cut or not cut, so we get a recursive function F(n)=max(i*(n-i),i*F(n-i)), i=1,2,...,n-2.
Code:

class Solution {
    
    
public:
    int cuttingRope(int n) {
    
    
    	if(n == 2) return 1;
    	int res = -1;
    	for(int i = 1;i<n;i++){
    
    
			res = max(res, max(i * cuttingRope(n-i),i*(n-1)) );
		}
		return res;
    }
};

Method 2: Memoization technology (top-down) The
above-mentioned violent solutions will time out, but many advanced solutions are often optimizations of violent solutions. Note that the reason for the timeout in the above code is mainly due to repeated calculation of F(n). In order to avoid repeated calculations, memoization can be used.
The code of mnemonic technology uses the array f to store the maximum length f[i] when the length is i, and finally returns f[n].
Code:

class Solution {
    
    
public:
    int cuttingRope(int n) {
    
    
    	if(n == 2) return 1;
    	if (f[n] != 0) // 如果f[n]已经计算过,直接返回避免重复计算
            return f[n];
    	int res = -1;
    	for(int i = 1;i<n;i++){
    
    
			res = max(res, max(i * cuttingRope(n-i),i*(n-1)) );
		}
		f[n] = res;
		return res;
    }
};

Memoized search is also called "memo method", It starts from F(n) in the tree structure similar to the above, and gradually recurses to the known value F(2).Understanding becomes a top-up solution

Dynamic Programming Solution See original blog: Author: z1m, source: power button

to sum up:

1. Recursive traversal is simple to implement and understand, and it is easier to use than for loop, but the time complexity is slightly higher (the constant coefficient part is large).
2. For recursive traversal, the solution of the sub-problem is usually calculated repeatedly, so the memory method is often used to save the solution of the sub-problem.

Guess you like

Origin blog.csdn.net/qq_33726635/article/details/106616700