LeetcodeMedium- [Interview Question 14- I. Cut the Rope]

To give you a rope of length n, please cut the rope into m lengths of integer length (m and n are integers, n> 1 and m> 1), the length of each length of rope is denoted as k [0], k [1] ... k [m]. What is the maximum possible product of k [0] * k [1] * ... * k [m]? For example, when the length of the rope is 8, we cut it into three sections of length 2, 3, and 3, respectively, and 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


prompt:

2 <= n <= 58
Note: This question is the same as the main station 343 question: https://leetcode-cn.com/problems/integer-break/

Source: LeetCode
Link: https://leetcode-cn.com/problems/jian-sheng-zi-lcof The
copyright belongs to the deduction network. Please contact the official authorization for commercial reprint, and please indicate the source for non-commercial reprint.

Idea 1: Violence

For each number minus 1, start to test whether to continue disassembling after subtracting each number, and see which one has the largest value.

14.png

This will directly calculate a lot of duplicate values, so you can consider using a memory array to mark the number that has been calculated.

class Solution:
    def cuttingRope(self, n: int) -> int:
        def fun(n):
            if n == 2:
                return 1
            if f[n] != 0:
                return f[n]
            res = -1
            for i in range(1, n):
                res = max(res, max(i * fun(n - i),i * (n - i)))
            f[n] = res
            return res
        # 记忆数组
        f = [0 for _ in range(n+1)]
        return fun(n)

Idea 2: Dynamic programming (dp)

It can be done using dynamic programming. The dynamic conversion formula is: dp [i] = max (dp [i], dp [j] * dp [i-j], where i is the current length and j is the subtracted length.

It should be noted that because the title requires m> 1, it must be cut, then for 2 and 3, there is an impact, special judgment is required, and as a part of others cut dp [2] = 2, dp [3] = 3, in order to make others as large as possible.

class Solution:
    def cuttingRope(self, n: int) -> int:
        if n == 2:
            return 1
        elif n == 3:
            return 2
        dp = [0 for _ in range(59)]
        dp[2] = 2 # 作为分割后的单元时可以不切
        dp[3] = 3
        for i in range(4, n+1):
            for j in range(1, i//2+1):
                dp[i] = max(dp[i], dp[i-j] * dp[j])
        return dp[n]

dp another way of writing

class Solution:
    def cuttingRope(self, n: int) -> int:
        dp = [0 for _ in range(n + 1)]  # dp[0] dp[1]其实没用
        dp[2] = 1  # 初始化
        res = -1
        for i in range(3, n + 1):
            for j in range(i):
                dp[i] = max(dp[i], max((i - j) * j, j * dp[i - j]))
        return dp[n]

作者:z1m
链接:https://leetcode-cn.com/problems/jian-sheng-zi-lcof/solution/xiang-jie-bao-li-di-gui-ji-yi-hua-ji-zhu-dong-tai-/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

 

 
Published 314 original articles · 22 praises · 20,000+ views

Guess you like

Origin blog.csdn.net/qq_39451578/article/details/105449475