一、题目地址
https://leetcode-cn.com/problems/jian-sheng-zi-lcof/
二、具体代码
/**
* @param {number} n
* @return {number}
*/
// 时间复杂度:O(loga)
// 空间复杂度:O(1)
var cuttingRope = function(n) {
if(n <= 3) {
return n - 1;
}
let a = Math.trunc(n / 3);
let b = Math.trunc(n % 3);
if(b === 0) {
// Math.pow()的时间复杂度为O(loga)
return Math.pow(3, a);
}
if(b === 2) {
return Math.pow(3, a) * 2;
}
return Math.pow(3, a - 1) * 2 * 2;
};