(dp)343. Integer Break

Posted cKK

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了(dp)343. Integer Break相关的知识,希望对你有一定的参考价值。

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]; } }

  

以上是关于(dp)343. Integer Break的主要内容,如果未能解决你的问题,请参考以下文章

*leetcode 343. 整数拆分 & 剑指offer 14

343. Integer Break

LeetCode 343. Integer Break

343. Integer Break

343. Integer Break

Leetcode 343. Integer Break