割绳子、整数拆分

![在这里插入图片描述](https://img-blog.csdnimg.cn/20200420180346541.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nNzZG4ubmV0L3FxXzQzMzkwMjM1,size_16,color_FFFFFF,t_70在这里插入图片描述

class Solution {
    public int cuttingRope(int n) {
        if(n <= 3) return n - 1;
        long res=1;
        while(n>4){
            res*=3;
            res=res%1000000007;
            n-=3;
        }
        return (int)(res*n%1000000007);
    
    }
}

在这里插入图片描述

一类型问题,主要明白3是最优解,2其次,1最差。
如何得出呢?看这里

class Solution {
    public int integerBreak(int n) {
        if(n<=3){return n-1;}
        int a=1;
        while(n>4){
            n-=3;
            a*=3;
        }
        return a*n;
    }
}
发布了45 篇原创文章 · 获赞 110 · 访问量 58万+

猜你喜欢

转载自blog.csdn.net/qq_43390235/article/details/105641029