[Rookie Training] Sword Finger Offer 14. Cut the Rope

Title description:

Here is a rope with a length of 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 k[0],k [1]...k[m-1]. What is the maximum possible product of k[0] k[1] …*k[m-1]? 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.

Example 1:
Input: 2
Output: 1
Explanation: 2 = 1 + 1, 1 × 1 = 1

Example 2:
Input: 10
Output: 36
Explanation: 10 = 3 + 3 + 4, 3 × 3 × 4 = 36
Tip:
2 <= n <= 58
Source: LeetCode
Link: https://leetcode- The
copyright of cn.com/problems/jian-sheng-zi-lcof belongs to Lingkou Network. For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.

Problem-solving ideas:

We assume that it is divided into m parts, the requirement of the question is to find
MAX(a(1) a(2)a(m)) is
known, n=a(1)+a(2)+…+a( m)

So when we intercept, how can we maximize the length of each segment after interception?
From the arithmetic geometric mean inequality, we know:
Quote from others

The product is the largest when we divide it into multiple segments with the same length.
Of course, it cannot be divided into 1 subsections.

Then we might as well assume that each paragraph is of equal length and not equal to 1.

Insert picture description here

Through the above proof, we know that when we cut a length of 3 each time, the final product obtained is the largest. But the reality is that we may not be able to cut all equal parts in the end, so there are three situations, that is, the last paragraph is the case of
length 0, 1, and 2: (1) The length is 0, the most perfect situation, Divide into m small ropes with a length of 3
(2) The length is 1, any number multiplied by 1 is itself and will not become larger, so we might as well combine 1 with 3 in the previous paragraph, so that the product obtained will be the largest
(3) The length is 2, we can't divide it anymore, just multiply it.

Note: This is mainly a greedy thought, looking for a local optimal solution every time.
I feel that dp can also be written and added later

Code:

public class jianzhi_Offer_14_1 {
    
    
    public int cuttingRope(int n) {
    
    
        if(n == 2){
    
    
            return 1;
        }
        if(n == 3){
    
    
            return 2;
        }
        if (n == 4){
    
    
            return 4;
        }
        int ans = 1;
        int tmp;
        int num = n;
        while (num >= 3){
    
    
            if((num - 3) >= 3){
    
    
                ans *= 3;
                num -= 3;
            }
            else if((num - 3) == 1){
    
    
                ans *= 4;
                num -= 4;
                break;
            }
            else if((num - 3) == 2){
    
    
                ans *= 6;
                num -= 5;
                break;
            }
            else if((num - 3) == 0){
    
    
                ans *= 3;
                num -= 3;
                break;
            }

        }
        return ans;
    }

    public static void main(String[] args) {
    
    
        jianzhi_Offer_14_1 obj = new jianzhi_Offer_14_1();
        System.out.println(obj.cuttingRope(9));
    }
}

Title description:

Here is a rope with a length of 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 k[0],k [1]...k[m-1]. What is the maximum possible product of k[0] k[1] …*k[m-1]? 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.
The answer needs to be modulo 1e9+7 (1000000007). If the initial result of the calculation is: 1000000008, please return 1.

Example 1:
Input: 2
Output: 1
Explanation: 2 = 1 + 1, 1 × 1 = 1

Example 2:
Input: 10
Output: 36
Explanation: 10 = 3 + 3 + 4, 3 × 3 × 4 = 36

Tip:
2 <= n <= 1000

Source: LeetCode
Link: https://leetcode-cn.com/problems/jian-sheng-zi-ii-lcof
Copyright is owned by LeetCode . For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.

Problem-solving ideas:

The difference between this question and the former is that the value range of n becomes larger, which means that the final answer we get will exceed the maximum limit that int can withstand. At this time, we can use the distributiveness of the remainder:
(a * b)% m = ((a%m)*(b%m))% m
plus the cyclic remainder to solve the problem. (Because, the limit may be exceeded during the intermediate multiplication, so we need to multiply to take the remainder of the result)

Code:

public class jianzhi_Offer_14_2 {
    
    
    public int cuttingRope(int n) {
    
    
        int modnum = 1000000007;
        if(n == 2){
    
    
            return 1;
        }
        if(n == 3){
    
    
            return 2;
        }
        if (n == 4){
    
    
            return 4;
        }
        //注意这里要使用long
        long ans = 1;
        int tmp;
        int num = n;
        while (num >= 3){
    
    
            ans = ans % modnum;
            if((num - 3) >= 3){
    
    
                ans = (ans * 3) % modnum;
                num -= 3;
            }
            else if((num - 3) == 1){
    
    
                ans = (ans * 4) % modnum;
                num -= 4;
                break;
            }
            else if((num - 3) == 2){
    
    
                ans = (ans * 6) % modnum;
                num -= 5;
                break;
            }
            else if((num - 3) == 0){
    
    
                ans = (ans * 3) % modnum;
                num -= 3;
                break;
            }

        }
        if( ans == modnum + 1)
            return 1;
        return (int)ans;
    }

    public static void main(String[] args) {
    
    
        jianzhi_Offer_14_2 obj = new jianzhi_Offer_14_2();
        System.out.println(obj.cuttingRope(120));
    }
}

Guess you like

Origin blog.csdn.net/Puppet__/article/details/115076052