Analysis of the Maximum Product in the "to be filled"

Source: Internet
Author: User

Original question

A rope of N meters in length. Cut it into several segments. The length of each segment is an integer. A cutting method is provided to ensure that the product between each segment of the cut rope is the largest. Note: you must cut at least.

Analysis

How can I analyze this question step by step? No matter how many segments are cut, there will always be the first segment, the second segment... And so on. What are the choices for the length of the first segment? It can be 1, 2, 3... Until N-1 (cut at least). We use max_prod (n) to represent the maximum value of the product in the cut method of the rope whose length is N. So:

1. when the first segment is 1, the maximum product is: max (1 × Max _ prod (n-1), 1 × (n-1) 2. when the first segment is 2, the maximum product is: max (2 × Max _ prod (n-2), 2 × (n-2) 3.... 4. When the first segment is n-1, the maximum product is: (n-1) * 1 = n-1

Why is the above Max? Note that the requirements in the questions should be at least cut. In this way, we can see from the above analysis that the recursive expression sets the length of the first line to I and the value range to [1, n-1]. Then, for each I, the maximum product is max (I x (n-I), I x max _ prod (n-I )). Then the maximum value for all I is the final answer.

Is this question finished here? After reading our previous analysis, I can quickly think of it and check whether there are duplicates in these subquestions. If so, I can use the dynamic planning method to modify the algorithm. To check whether there are repeated recursive subproblems, a better method is to draw a recursive tree on the paper and check whether there are repeated recursive subproblems. It is clear. This question is obvious. Students can try to draw and Practice on paper.

Now that we have fixed the recurrence subproblem, what then? I believe that those who have studied the dynamic planning topic will remember the following sentence: "top-down problem analysis, bottom-up problem solving", which is similar, probably not the original story. Solving the problem from the bottom up means solving small problems first, and then solving a batch of problems based on the results of these small problems until the whole problem is solved:

1. when the length of the rope is 1, ignore it and cannot cut 2. when the length of the rope is 2, cut a knife, max_prod [2] = 1 × 13. when the length of the rope is 3, cut a knife, max_prod [3] = 1 × 24. when the length of the rope is 4, it depends on the cutting method of each of the previous lengths, and the first length starts from 2. 5 ....

The state transition equation can be expressed as: max_prod [I] = max (I-j) × J, J × Max _ prod [I-j]). the range of J is [1, I/2]. Obviously, the time complexity of the dynamic planning method is O (n ^ 2), and the space complexity is O (n ).

Int maxmult (int n) {vector <int> dp (n + 1, 1); int Len, I; for (LEN = 3; Len <= N; Len ++) {for (I = 1; I <Len; I ++) {int T = max (DP [Len-I], len-I) * I; // DP [Len-I] may be smaller than (LEN-I), because DP [Len-I] indicates the maximum length of the split product of len-I, however, len-I is not separated, and the two do not overlap DP [Len] = max (DP [Len], T) ;}} return DP [N];}

This is a good question. However, this question is more clever. On Weibo, some people provide the following method: you only need to express the length N as 3X + 2y = N, and 3 as much as possible, 3 ^ x + 2 ^ y is the largest. I have to be amazed. This is indeed a clever method. You can use examples to verify the number of instances. Why is there only 3 and 2? 2x2, 5 or above, which can be divided into 3 x + 2y and 3 x + 2 ^ y> 5. This question must be an integer. What if I cancel this restriction? To expand your thinking, please think more.

Analysis of the Maximum Product in the "to be filled"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.