[Math_Medium]343. Integer Break

原题: 343. Integer Break

题目大意:给你一个数(2-57), 将这个数拆成若干项(和不变),使其乘积最大。

解题思路:

我们知道,一个数,无论拆成多少项,当每一项都相等时,其乘积最大;假设我们将N拆成N/x个x,于是其乘积就是 x^(N/x),我们对这个乘积求导:

可知,当x=e时,倒数为0,此时函数取得最大值,而我们在这里只能取整数,所以可以取2或者3,但是我们发现 2x2x2 < 3x3,所以我们应该尽可能地取3,所以代码如下:

源代码:

class Solution{
    public:
    int integerBreak(int n)
    {
        if(n==2)
            return 1;
        else if(n==3)
            return 2;
        else
        {
            int ans=1;
            while(n>4)
            {
                ans*=3;
                n-=3;
            }
            ans*=n;
            return ans;
        }
    }
};

这里也可以使用动态规划的方法:

设dp[i]为数值 i 拆分后的最大值,我们就取max(dp[i], dp[i-j] * j)

代码如下:

class Solution{
    public:
    int integerBreak(int n)
    {
          vector<int> dp[n+1,1];
          for(int i=3;i<dp.size();i++)
              for(int j=2;j<i;j++)
                    {
                          if(dp[i]<(dp[i-j]*j))
                              dp[i]=dp[i-j]*j;
                          if(dp[i]<i*(i-j))//因为初始化值为1,所以要先一个一个拆开赋值
                               dp[i]=i*(i-j);
                   }
               return dp[n];
    }
};

以上

原文地址:https://www.cnblogs.com/qiulinzhang/p/9514441.html

时间: 2024-11-05 16:25:05

[Math_Medium]343. Integer Break的相关文章

[email&#160;protected] [343] Integer Break (Math &amp; Dynamic Programming)

https://leetcode.com/problems/integer-break/ Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get. For example, given n = 2, return 1 (2

343. Integer Break

Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get. For example, given n = 2, return 1 (2 = 1 + 1); given n = 10, return 36 (10 = 3 +

leetcode 343. Integer Break(dp或数学推导)

Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get. For example, given n = 2, return 1 (2 = 1 + 1); given n = 10, return 36 (10 = 3 +

(dp)343. Integer Break

Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get. For example, given n = 2, return 1 (2 = 1 + 1); given n = 10, return 36 (10 = 3 +

LeetCode 343. Integer Break

https://leetcode.com/problems/integer-break/ 数学题. 1 #include <iostream> 2 using namespace std; 3 4 class Solution { 5 public: 6 int integerBreak(int n) { 7 if (n == 2) return 1; 8 if (n == 3) return 2; 9 10 int product = 1; 11 while (n > 4) 12 {

[LeetCode][Python]Integer Break

Integer Break Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get. For example, given n = 2, return 1 (2 = 1 + 1); given n = 10, return

LeetCode - Integer Break

Integer Break Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get. For example, given n = 2, return 1 (2 = 1 + 1); given n = 10, return

Integer Break

Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get. For example, given n = 2, return 1 (2 = 1 + 1); given n = 10, return 36 (10 = 3 +

【Leetcode】Integer Break

题目链接:https://leetcode.com/problems/integer-break/ 题目: Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get. For example, given n = 2, re