题目链接:https://leetcode.com/problems/integer-break/description/

题目大意:给定一个自然数,将其分解,对其分解的数作乘积,找出最大的乘积结果。例子如下:

法一(借鉴):dp,一维dp,dp[i]表示i的最大分解结果的乘积,而dp[5]可以dp[4],dp[3],dp[2],dp[1]为基础,dp[4]可以dp[3],dp[2],dp[1]为基础,代码如下(耗时1ms):

     //dp[i]表示给定i分解后能得到的最大乘积值
public int integerBreak(int n) {
int dp[] = new int[n + 1];
dp[1] = 1;
dp[2] = 1;
for(int i = 3; i <= n; i++) {
for(int j = 1; j <= i / 2; j++) {
//max里面要加上dp[i],因为里层for循环会不断更新dp[i],否则dp[i]得到就是最后一次的计算结果,而取不到最大值
//后面Math.max(j, dp[j]) * Math.max(i - j, dp[i - j]),因为j+(i-j)=i,所以计算j和i-j的乘积,是正常的,只不过这里可以用到先前已经算过的dp[j]和dp[i-j],因为dp[j]的结果就是j的最大分解结果,那么也可以是i的分解结果
dp[i] = Math.max(dp[i], Math.max(j, dp[j]) * Math.max(i - j, dp[i - j]));
}
}
return dp[n];
}

法二(借鉴):数学方法,数学原理:https://leetcode.com/problems/integer-break/discuss/80721/Why-factor-2-or-3-The-math-behind-this-problem.,由数学知,一个整数分解,当分解成相同的数时,乘积最大,而由于给定的自然数不一定都能分解成相同的数,所以又由数学知,求导办法见https://www.cnblogs.com/zywscq/p/5415303.html,当分解得到的数越靠近e,得到的乘积值越大,那么也就是能取3则取3,不能则取2。而又如6=2+2+2=3+3,又2*2*2<3*3,所以当能分解成3个2时,应该换算成2个3,所以下面与3求余,然后分情况分解。代码如下(耗时0):

     public int integerBreak(int n) {
if(n == 2) {
return 1;
}
else if(n == 3) {
return 2;
}
else if(n == 1) {
return 1;
}
else if(n % 3 == 0) {
return (int)Math.pow(3, n / 3);
}
else if(n % 3 == 1) {
return 2 * 2 * (int)Math.pow(3, (n - 4) / 3);
}
else {
return 2 * (int)Math.pow(3, (n - 2) / 3);
}
}

343.Integer Break---dp的更多相关文章

  1. leetcode 343. Integer Break(dp或数学推导)

    Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...

  2. [LeetCode]Integer Break(Dp或胡搞或推公式)

    343. Integer Break Given a positive integer n, break it into the sum of at least two positive intege ...

  3. LN : leetcode 343 Integer Break

    lc 343 Integer Break 343 Integer Break Given a positive integer n, break it into the sum of at least ...

  4. #Week 11 - 343.Integer Break

    Week 11 - 343.Integer Break Given a positive integer n, break it into the sum of at least two positi ...

  5. 【LeetCode】343. Integer Break 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学解法 动态规划 日期 题目地址:https:// ...

  6. (dp)343. Integer Break

    Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...

  7. [LeetCode] 343. Integer Break 整数拆分

    Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...

  8. Leetcode 343. Integer Break

    Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...

  9. 343. Integer Break

    Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...

  10. leetcode@ [343] Integer Break (Math & Dynamic Programming)

    https://leetcode.com/problems/integer-break/ Given a positive integer n, break it into the sum of at ...

随机推荐

  1. Struts按着配置文件的加载的顺序,后面文件和前面文件相同的配置,后面的会把前面的文件的值覆盖

    Struts按着配置文件的加载的顺序,后面文件和前面文件相同的配置,后面的会把前面的文件的值覆盖

  2. dom变成jquery对象 先获取dom对象 然后通过$()转换成jquery对象

    dom变成jquery对象   先获取dom对象 然后通过$()转换成jquery对象

  3. Contest 9

    A:搜索好难啊根本不会啊. B:暴力枚举给哪段前缀乘,维护一些前后缀最大最小值之类的东西就很好算了. #include<iostream> #include<cstdio> # ...

  4. Educational Codeforces Round 33 (Rated for Div. 2) 题解

    A.每个状态只有一种后续转移,判断每次转移是否都合法即可. #include <iostream> #include <cstdio> using namespace std; ...

  5. 如何用Qt Python创建简单的桌面条形码应用

    Qt for Python可以快速跨平台的GUI应用.这篇文章分享下如何结合Dynamsoft Barcode Reader SDK来创建一个简单的读码应用. 安装Qt for Python 官方站点 ...

  6. URAL1519:Formula 1——题解

    http://acm.timus.ru/problem.aspx?space=1&num=1519 https://vjudge.net/problem/URAL-1519 题目大意:给一个网 ...

  7. Oracle10g数据泵impdp参数详解--摘自网络

    Oracle10g数据泵impdp参数详解 2011-6-30 12:29:05 导入命令Impdp •      ATTACH 连接到现有作业, 例如 ATTACH [=作业名]. •      C ...

  8. K8s仪表盘

    { "__inputs": [ { "name": "DS_TEST-ENVIORMENT-K8S", "label": ...

  9. width: calc(100% - 20px);

    width: calc(100% - 20px); css3 的 calc()函数.这里的意思是设置宽度比100%的宽度少20px. calc()函数用于动态计算长度值. calc()函数支持 &qu ...

  10. tp 中 where条件,字段和字段的大小比较

    $map = array( , 'start_time' => array('lt',$now), 'end_time' => array('gt',$now), , '_string' ...