class Solution {
public int integerBreak(int n) {
if (n <= 2) return 1;
if (n 3) return 2;
int res = n / 3;
int mod = n % 3; // 尽可能分成长度为3的res段
if (mod 0) { // 正好拆分为整数个3
return (int)Math.pow(3, res);
} else if (mod 1) {
return (int)(Math.pow(3, res – 1) * 4);
} else {
return (int)(Math.pow(3, res) * 2);
}
}
}
class Solution {
// 普通方法
// long pow(int a, int n){
// long res = 1;
// for(int i = 1; i <= n; i++){
// res = res * a;
// }
// return res;
// }
//快速幂方法
long pow(int a, int n){
long res = 1;
long y=n;
long temp=a;
while(y>0){
if((y&1)==1){
res*=temp;
}
temp*=temp;
y=y>>1;
}
return res;
}
public int cuttingBamboo(int bamboo_len) {
int n = bamboo_len;
if(n <= 2){
return 1;
}
if(n <= 3){
return 2;
}
int res = n / 3;
int mod = n % 3;
if(mod == 0){
return (int)pow(3, res);
}else if(mod == 1){
return (int)(pow(3, res - 1) * 4 );
}else{
return (int)(pow(3, res) * 2 );
}
}
}
class Solution {
public:
int cuttingBamboo(int bamboo_len) {
if(bamboo_len>3){
int a = bamboo_len/3, b = bamboo_len%3;
switch(b){
case 0: return pow(3,a);
case 1: return pow(3,a-1)*4;
case 2: return pow(3,a)*2;
}
}
return bamboo_len-1;// bamboo_len<=3,绳子必须被切割,长度为2的时候,被切割为1*1,长度为3的时候,被切割为1*2
}
};
class Solution:
def cuttingBamboo(self, bamboo_len: int) -> int:
if bamboo_len <= 3: return bamboo_len – 1
a, b = bamboo_len // 3, bamboo_len % 3
if b 0: return int(math.pow(3, a))
if b 1: return int(math.pow(3, a – 1) * 4)
return int(math.pow(3, a) * 2)
public int cuttingRope(int n) {
if(n <= 2){//判断特殊情况
return 1;
}
if(n == 3){
return 2;
}
int res = n / 3;
int mod = n % 3;
if(mod == 0){ //正好是3的倍数
return pow(3, res);
} else if(mod == 1){ //余数为1,把1加到一个3里面
return pow(3, res - 1) * 4;
} else {//余数为2,相乘
return pow(3, res) * 2;
}
}
int pow(int a, int n){
int sum = 1;
for(int i = 1; i <= n; i ++){
sum = sum * a;
}
return sum;
}
帅地的公式解法:
class Solution {
int result = 1;
int max = 0;
int count = 1;
public int cuttingBamboo(int bamboo_len) {
if(bamboo_len<2){
return 1;
}
if(bamboo_len==3){
return 2;
}
int res = bamboo_len/3;
int mod = bamboo_len%3;
if(mod==0){
return pow(3,res);
}else if(mod == 1){
return pow(3,res-1)*4;
}else{
return pow(3,res)*2;
}
}
public int pow(int n,int m){
int result = 1;
for(int i = 0;i<m;i++){
result*=n;
}
return result;
}
}
自己本来想用回溯法来解的 但是如果bamboo_len比较大的时候(28),这时候直接超时了,但是在idea里能输出正确答案,不过idea里也会稍微卡一下才出答案,可能是因为没剪枝的问题,这里还是记录一下回溯:
public class Solution {
int result = 1;
int max = 0;
int count = 1;
public int cuttingBamboo(int bamboo_len) {
backtrace(bamboo_len);
return max;
}
public void backtrace(int bamboo_len){
if(bamboo_len<1){
if(max<result){
max = result;
}
return;
}
for(int i = 1;i<=bamboo_len;i++){
if(i==bamboo_len&&count==1){
break;
}
result*=i;
count++;
backtrace(bamboo_len-i);
result/=i;
count--;
}
}
}
共以下 21 个回答
class Solution {
public int cuttingBamboo(int bamboo_len) {
int n = bamboo_len;
}
时间:O(n2)
空间:O(n)
class Solution {
public int integerBreak(int n) {
if (n <= 2) return 1;
if (n 3) return 2;
int res = n / 3;
int mod = n % 3; // 尽可能分成长度为3的res段
if (mod 0) { // 正好拆分为整数个3
return (int)Math.pow(3, res);
} else if (mod 1) {
return (int)(Math.pow(3, res – 1) * 4);
} else {
return (int)(Math.pow(3, res) * 2);
}
}
}
时间复杂度:本题的时间主要花在 pow 函数上,采用快速幂可以 logn 的时间复杂度
空间:O(1)
时间复杂度$O(1)$,空间复杂度$O(1)$
class Solution {
public:
int cuttingBamboo(int bamboo_len) {
if(bamboo_len <= 3) return bamboo_len -1;
int a = bamboo_len /3;
int b = bamboo_len %3;
if(b 0) return pow(3, a);
if(b 1) return pow(3, a-1)4;
return pow(3, a)2;
}
};
时间复杂度和空间复杂度都是O(1)
思路:数学推导
public int cuttingBamboo(int bamboo_len) {
if(bamboo_len<=3) return bamboo_len-1;
int a=bamboo_len/3,b=bamboo_len%3;
if(b0) return (int)Math.pow(3,a);
if(b1) return (int)Math.pow(3,a-1)4;
return (int)Math.pow(3,a)2;
}
思路:数学问题 均值不等式和函数求导确定
java
class Solution{
public int cuttingBamboo(int bamboo_len){
if(bamboo_len <= 3) return bamboo_len – 1;
int a = bamboo_len / 3, b = bamboo_len % 3;
if(b 0) return (int)Math.pow(3, a);
if(b 1) return (int)Math.pow(3, a – 1) * 4;
return (int) Math.pow(3, a) * 2;
}
}
提交
时间复杂度O(1)
空间复杂度O(1)
“`C++
class Solution {
public:
int cuttingBamboo(int bamboo_len) {
if( bamboo_len < 4 ) { return bamboo_len - 1; } int a = bamboo_len/3; int b = bamboo_len%3; if(b == 0) { return pow(3, a); } if(b == 1) { return pow(3, a - 1) * 4; } return pow(3, a) * 2; } }; ```
class Solution { dp(bamboo_len +1);
public:
int cuttingBamboo(int bamboo_len) {
//dp[i] 表示将正整数 i拆分成至少两个正整数的和之后,这些正整数的最大乘积。
vector
for(int i=2; i<=bamboo_len;i++){
int curMax = 0;
for(int j =1;j<i;j++){//从1开始拆分,
curMax = max(curMax, max(j(i-j), jdp[i-j]));
}
dp[i] = curMax;
};
class Solution {
public:
int cuttingBamboo(int bamboo_len) {
if(bamboo_len <= 3) return bamboo_len – 1;
int x = bamboo_len / 3, y = bamboo_len % 3;
if(y 0) return pow(3, x);
if(y 1) return pow(3, x – 1) * 4;
return pow(3, x) * 2;
}
};时间复杂度O(1),
class Solution {
public int cuttingBamboo(int bamboo_len) {
//dp
int[] dp = new int[bamboo_len + 1];
dp[0] = 0;
dp[1] = 1;
for(int i = 2; i < bamboo_len+1; i++){
int maxLen = 0;
for(int j = 1; j < i; j++){
maxLen = Math.max(maxLen,Math.max(j*(i-j),j * dp[i – j]));
}
dp[i] = maxLen;
}
}//方法一 dp 时间复杂度On2 空间复杂度On
class Solution {
public int cuttingBamboo(int bamboo_len) {
if(bamboo_len <= 3)
return bamboo_len – 1;
int v1 = bamboo_len % 3;
int v2 = bamboo_len / 3;
if(v1 0)
return (int) Math.pow(3,v2);
else if(v1 1)
return (int) Math.pow(3,v2 – 1)*4;
return (int) Math.pow(3,v2) * 2;
}
}//数学法 时间复杂度O1 空间复杂度O1
打卡
class Solution:
def cuttingBamboo(self, bamboo_len: int) -> int:
if bamboo_len <= 3: return bamboo_len – 1
a, b = bamboo_len // 3, bamboo_len % 3
if b 0: return int(math.pow(3, a))
if b 1: return int(math.pow(3, a – 1) * 4)
return int(math.pow(3, a) * 2)
关于为什么切分为3的优先级最高 可以利用均值不等式求出乘积最大值 L(m)=(n/m)^m 对此式求导(可利用对数法),可以证明当 m=n/e 时,乘积取最大,此时每段绳子的长度为 n/(n/e)=e,自然对数e的值为2.718,显然接近3,所以总体来讲3最好
时间复杂度O(N2)
空间复杂度O(N)