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 + 3 + 4).less
Note: you may assume that n is not less than 2.this
Hint:spa
题意:给一个n(n>=2),求相加等于n且乘积最大的一组整数的积。code
题解:blog
这其实就是一道高中数学题,可是leetcode上的数据比较水,致使彻底不用数学推导的O(n2)的dp也能够过。leetcode
解法一(纯dp):get
令dp[n]为n对应的最大积。数学
那么递推方程就是:dp[n]=max(i*dp[n-i],i*(n-i))(其中i从1到n-1)。it
边界:dp[2]=1;
时间复杂度:O(n2)
class Solution { public: int integerBreak(int n) { int dp[n]; dp[1]=1; dp[2]=1; for(int i=3;i<=n;i++){ dp[i]=-1; for(int j=1;j<i;j++){ dp[i]=max(j*dp[i-j],max(dp[i],j*(i-j))); } } return dp[n]; } };
解法二(当成数学题来作就好):
由均值不等式(n个数的算术平均数大于等于它们的几何平均数):
>=
得:当把输入的n拆分红几个相等的数时它们的积最大。
那么问题来了,拆分红几个呢?
为了方便使用导数,咱们先假设咱们能够把n拆分红实数。那么设每个数为x,则一共有n/x个数。
设它们的积为f(x),则f(x)=x(n/x),那么怎么求f(x)最大值呢?求导数!
f′(x)=(n/x2) * x(n/x) * (1-lnx)
当x=e时取极大值。
而咱们题目里规定x为整数,那么咱们只须要取的x越靠近e越好。那么2<e<3,并且e=2.71828...,因此取3是最好的,若是取不到3就取2。
幂运算复杂度为O(lgn),因此这个算法复杂度为O(lgn)。
class Solution { public: int integerBreak(int n) { if(n == 2) return 1; else if(n == 3) return 2; else if(n%3 == 0) return pow(3, n/3); else if(n%3 == 1) return 2 * 2 * pow(3, (n - 4) / 3); else return 2 * pow(3, n/3); } };