方法
算法思想:① 当所有绳段长度相等时,乘积最大。② 最优的绳段长度为 3 。
时间复杂度:O(1)
空间复杂度:O(1)
边界条件:
补充知识:数学推导
class Solution {
public int cuttingRope(int n) { //一定要返回int
if(n<=3) return n-1; //题目要求必须>1
int a=n/3,b=n%3;
if(b==0) return (int)Math.pow(3,a); //不剩余
if(b==1) return (int)Math.pow(3,a-1)*4; //1+3 拆成2+2
return (int)Math.pow(3,a)*2; //剩余2
}
}