Share my simple C++ solution


  • 0
    H
    class Solution {
    public:
    int integerBreak(int n) {
    
        //n<=4, consider separately
        if(n <= 3) return n-1;
        if(n == 4) return n;
    
        //As much as possible into 3, if 3 is an aliquant part of n, using 2/4 to fill up
        int k = n/3;
        int s = n%3;
        int product = 1;
    
        for(int i=0;i<k;i++)
            product *= 3;
    
        if(s == 0)
            return product;
        else if(s == 2)
            return product*2;
        else{
            product = (product/3)*4;   
            return product;
        }
    }
    };

  • 0
    H

    (o.o) excellent thinking,and what a pretty code.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.