【剑指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);
    }
};
posted @ 2020-05-08 19:54  NaughtyCoder  阅读(69)  评论(0)    收藏  举报