C++ Solution using DP with O(n^2) time complexity but 0ms and 9 lines code


  • 0
    Y
    class Solution {
    public:
        int integerBreak(int n) {
            vector<int> dp(n + 1);
            dp[1] = dp[2] = 1;
            for(int i = 1; i <= n; ++ i){
                for(int j = 1; j <= n - i; ++ j){
                    dp[i + j] = max(dp[i + j], dp[i]*j);
                    dp[i + j] = max(dp[i + j], i*j);
                }
            }      
            return dp[n];
        }
    };

Log in to reply
 

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