其他分享
首页 > 其他分享> > 【剑指offer】【贪心】14- II. 剪绳子

【剑指offer】【贪心】14- II. 剪绳子

作者:互联网

题目链接:https://leetcode-cn.com/problems/jian-sheng-zi-ii-lcof/

贪心

class Solution {
public:
    int cuttingRope(int n) {
        if(n <= 3) return 1 *(n - 1);

        long long res = 1;
        if(n % 3 == 1) res = 4, n -= 4;
        else if(n % 3 == 2) res = 2, n -= 2;

        while(n) res *= 3, res %= 1000000007, n -= 3;
        
        return res % (1000000007);
    }
};

标签:ii,cn,zi,jian,offer,int,II,14,贪心
来源: https://www.cnblogs.com/Trevo/p/12739971.html