题目链接: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. 洛谷 P3177 树上染色

    题面 题目要求将k个点染成黑色,求黑点两两距离及白点两两距离,使他们之和最大. 我们可以将距离转化为路径,然后再将路径路径拆分成边,就可以记录每条边被经过的次数,直接计算即可. 很简单对吧?那么问题来 ...

  2. Django模板语言循环字典

    1. 对于字典,可以有下列用法: {% for row in user_dict.keys %} {% for row in user_dict.values %} {% for row in use ...

  3. Elasticsearch之基本操作

    elasticsearch是一个是开源的(Apache2协议),分布式的,RESTful的,构建在Apache Lucene之上的的搜索引擎. 它有很多特点例如Schema Free,Document ...

  4. BZOJ3530:[SDOI2014]数数——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=3530 我们称一个正整数N是幸运数,当且仅当它的十进制表示中不包含数字串集合S中任意一个元素作为其子 ...

  5. 洛谷P2125图书馆书架上的书 题解报告

    题目描述 图书馆有n个书架,第1个书架后面是第2个书架,第2个书架后面是第3个书架……第n-1个书架后面是第n个书架,第n个书架后面是第1个书架,第i个书架上有b[i]本书.现在,为了让图书馆更美观, ...

  6. Linux内核分析5

    周子轩 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 一.学习总结 通过gdb ...

  7. Spring MVC POJO入参过程分析

    SpringMVC确定目标方法POJO类型的入参过程 1.确认一个key: (1).若目标方法的POJO类型的参数没有使用@ModelAttribute作为修饰,则key为POJO类名第一个字母的小写 ...

  8. hiho 1044 : 状态压缩

    #1044 : 状态压缩·一 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho在兑换到了喜欢的奖品之后,便继续起了他们的美国之行,思来想去,他们决定乘坐火车 ...

  9. laravel添加日常备份任务

    app/Console/Command/MySqlDump.php <?php namespace App\Console\Commands; use Illuminate\Console\Co ...

  10. SpringBoot项目中使用swagger2暴露resftul接口增加JWT来进行安全性验证

    首先推荐两篇文章: 关于保护RestAPI的一些介绍: http://www.jianshu.com/p/6307c89fe3fa token与session的一些区别漫谈: http://www.j ...