Hint for the pattern.


  • 4

    As we can see the pattern is, for n>=4:

    4=2+2;
    5=2+3;
    6=3+3;
    7=2+2+3;
    8=2+3+3;
    9=3+3+3;
    10=2+2+3+3;
    11=2+3+3+3;
    12=3+3+3+3;
    13=2+2+3+3+3;
    14=2+3+3+3+3;
    15=3+3+3+3+3;
    

    and so on.

    If n == 3 * k then the answer is 3 ^ k;

    If 3*(k-1) < n <= 3*k, replace some 3 with 2's.


  • 0
    B

    using basic math

    class Solution {
    public:
        int integerBreak(int n) {
            
            if(n == 2)
            {
                return 1;
            }
            if(n ==3 )
                return 2;
            if(n%3 == 0)
            {
                int temp = n/3;
                return pow(3,temp);
            }
            else if(n%3 == 1)
            {
                int temp = (n/3)-1;
                return pow(3,temp)*4;
            }
            else
            {
                int temp = n/3;
                return pow(3,temp)*2;
            }
        }
    };
    
    

Log in to reply
 

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