Share my O(n) C++ solution,easy to understand


  • 0
    V
    class Solution {
    public:
        int integerBreak(int n) {
            int inBreak[] = {1, 1, 2, 4};
            if (n < 5)
                return inBreak[n-1];
    
            int ret = 1;
            
            while (n > 4)
            {
                ret *= 3;
                n -= 3;
            }
                
            return ret * n;
        }
    };

Log in to reply
 

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