O(1) space and O(1) complexity 0ms C++ solution for it


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

    };


  • 0
    4

    ^ Pow runtime is hardware specific; you should not assume that it's constant.


Log in to reply
 

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