cKK

............当你觉得自己很辛苦,说明你正在走上坡路.............坚持做自己懒得做但是正确的事情,你就能得到别人想得到却得不到的东西............

导航

(dp)343. Integer Break

Posted on 2016-06-25 14:19  cKK  阅读(134)  评论(0编辑  收藏  举报

Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get.

For example, given n = 2, return 1 (2 = 1 + 1); given n = 10, return 36 (10 = 3 + 3 + 4).

Note: you may assume that n is not less than 2.


public class Solution { //dp public int integerBreak(int n) { if(n==2) return 1; if(n==3) return 2; int[] dp=new int[n+1]; dp[0]=dp[1]=1; dp[2]=2;dp[3]=3; for(int i=4;i<=n;i++){ dp[i]=dp[1]*dp[i-1]; for(int j=2;j<=i/2;j++){ dp[i]=(dp[i]>dp[j]*dp[i-j])?dp[i]:dp[j]*dp[i-j]; } } return dp[n]; } }