5 lines O(1) C++


  • 0
    D
    public:
        int integerBreak(int n) {
            if(n == 2) return 1;
            if(n == 3) return 2;
            if(n%3 == 1) return pow(2,2) * pow(3,n/3-1);
            if(n%3 == 2) return pow(2,1) * pow(3,n/3);
            return pow(3,n/3);
        }
    };

  • 0
    Y

    Very neat.
    You may save one more line like

    public:
        int integerBreak(int n) {
            if(n <= 3) return n-1;
            if(n%3 == 1) return pow(2,2) * pow(3,n/3-1);
            if(n%3 == 2) return pow(2,1) * pow(3,n/3);
            return pow(3,n/3);
        }
    };
    

Log in to reply
 

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