Closed-form solution


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

Log in to reply
 

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