这一题已经不能用动态规划了,取余之后max函数就不能用来比大小了。
只能用贪心了
这个解法特别清楚
class Solution {
public:
int cuttingRope(int n) {
if (n <= 3) return 1 * (n - 1); //长度小于等于3的情况
long long res = 1;
if (n % 3 == 1) res = 4, n -= 4; //余数为1且总长度大于3,则可以剪切成2*2即4,不能剪切成1*3
else if (n % 3 == 2) res = 2, n -= 2; //余数为2时表示多余一个2
while (n) res = ((long long)res * 3) % 1000000007, n -= 3; //剩下的就是3的整数倍,全部剪切成3
return res;
}
};
作者:wo-yao-chu-qu-luan-shuo
链接:https://leetcode-cn.com/problems/jian-sheng-zi-ii-lcof/solution/jian-sheng-zi-iishu-xue-wen-ti-by-wo-yao-zlre/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。