Simple Recursive O(n) C++ Solution


  • 1

    If you discover the pattern, you can easily see that the problem is recursive.

    class Solution {
    public:
        int integerBreak(int n) {
            if (n < 3) return 1;
            if (n == 3) return 2;
            return maxSumProd(n);
        }
        
        int maxSumProd(int n) {
            return (n <= 4)? n : 3 * maxSumProd(n-3);
        }
    };
    

Log in to reply
 

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