给你一根长度为 n 的绳子,请把绳子剪成整数长度的 m 段(m、n都是整数,n>1并且m>1),每段绳子的长度记为 k[0],k[1]…k[m-1] 。请问 k[0]k[1]…*k[m-1] 可能的最大乘积是多少?例如,当绳子的长度是8时,我们把它剪成长度分别为2、3、3的三段,此时得到的最大乘积是18。
示例 1:
输入: 2
输出: 1
解释: 2 = 1 + 1, 1 × 1 = 1
示例 2:
输入: 10
输出: 36
解释: 10 = 3 + 3 + 4, 3 × 3 × 4 = 36
提示:
2 <= n <= 58
解题思路:
Ⅰ 动态规划 定义dp[i]为长度为i的绳子通过剪断得到的最大积值
等式关系:dp[i] = max(dp[i],dp[j]*dp[i-j]); //每剪一次,将最大值存入dp[i]
Ⅱ 数学方法:取最多的3时取得最大值,遇4取4(4 .> 22分)
为什么不是2? 因为33分>222分
n = 3a + b(b =0or1or2) 利用除法取余 和幂函数pow(底数,幂次)可求得积
tips:注意234等不用分割反积而更大的特殊情况
class Solution {
public:
int cuttingRope(int n) {
/*
//动态规划法
if(n <= 3) return n - 1;
//定义dp[i]为长度为i的绳子通过剪断得到的最大积值
vector<int> dp(n+1);
dp[1] = 1;
dp[2] = 2;
dp[3] = 3;
//j为剪下一刀,左半边的长度
//当j<=3时,不剪会有更大的积,不按照dp[j]乘,直接按照j乘,为方便计算,上面重新定义了dp123
for(int i = 4;i <= n;++i)
{
for(int j = 1;j <= i/2;++j) //4*5和5*4是一样的,试剪一半即可
{
dp[i] = max(dp[i],dp[j]*dp[i-j]); //每剪一次,将最大值存入dp[i]
}
}
return dp[n];*/
//取最多的3时取得最大值,遇4取4
/*if(n <= 3) return n-1;
int maxValue = 1;
while(n > 4)
{
maxValue *= 3;
n -= 3;
}
return maxValue*n;*/
//直接用库函数
if(n <= 3) return n-1;
int a = n/3;
int b = n%3;
if(b == 1) return pow(3,a-1)*4;
else if(b == 0) return pow(3,a);
else return pow(3,a)*2;
}
};
★
当n值较大时
答案需要取模 1e9+7(1000000007),如计算初始结果为:1000000008,请返回 1
//贪心+快速幂or循环幂
class Solution {
public:
int cuttingRope(int n) {
/*
if(n <= 3) return n-1;
long maxValue = 1; //用long型返回时转换为int型
while(n > 4)
{
maxValue = (maxValue*3)%1000000007;
n -= 3;
}
return (int)(maxValue*n%1000000007);*/
/*if(n <= 3) return n-1;
int a = n/3;
int b = n%3;
if(b == 0) return (int)pow(3,a)%1000000007; //pow的返回值是double类型,不能进行取余操作
else if(b == 1) return 4*(int)pow(3,a-1)%1000000007;
else return 2*(int)pow(3,a)%1000000007;*/
//快速幂
if (n <= 3) {
return n - 1;
}
int a = n / 3;
int b = n % 3;
if (b == 2) {
return (int) (quickPow(3, a) * b % 1000000007);
} else {
return (int) ((quickPow(3, a - 1) * (b + 3)) % 1000000007);
}
}
// private long quickPow(int x, int n) {
// if (n == 0) {
// return 1;
// }
// long y = quickPow(x, n / 2);
// return (n & 1) == 1 ? (y * y * x) % 1000000007 : (y * y) % 1000000007;
// }
private long quickPow(int x, long n) {
long res = 1;
long tt = x;
while (n != 0) {
if ((n & 1) == 1) {
res *= tt;
res %= 1000000007;
}
tt *= tt;
tt %= 1000000007;
n /= 2;
}
return res;
}
}
}
};
//递归
/*class Solution {
public int cuttingRope(int n) {
return n <= 3 ? n-1 : (int)process(n);
}
public long process(long n){
return n > 4 ? (process(n-3)*3)%1000000007 : n;
}
}*/
//基于贪心的动态规划
/*class Solution {
public:
int cuttingRope(int n) {
vector<long> dp(1001,0);
dp[1]=1;
dp[2]=1;
dp[3]=2;
dp[4]=4;
dp[5]=6;
dp[6]=9;
for(int i=7;i<=n;i++){
dp[i]=(dp[i-3]*3)%1000000007;
}
return dp[n];
}
};*/