Easy understand O(n^2) DP solution


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

    Basically, you calculate each way to break the integer to get the max product.


Log in to reply
 

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