题目链接: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. Contest 3

    A:非常裸的dp. #include<iostream> #include<cstdio> #include<cstdlib> #include<cstrin ...

  2. 【刷题】HDU 6184 Counting Stars

    Problem Description Little A is an astronomy lover, and he has found that the sky was so beautiful! ...

  3. 利用MailSniper越权访问Exchange邮箱

    0x01 概述 Microsoft Exchange用户可以授权给其他用户对其邮箱文件夹进行各种级别的访问.例如,用户可以授予其他用户读取访问其收件箱中里面的电子邮件,但是要是用户(或Exchange ...

  4. 《Linux内核设计与实现》学习总结 Chap1~2

    第一章 Linux内核简介 一.历史 由于Unix系统设计简洁并且在发布时提供源代码,所以许多其他组织和团体都对它进了进一步的开发. Unⅸ虽然已经使用了40年,但计算机科学家仍然认为它是现存操作系统 ...

  5. 自动化测试常用断言的使用方法(python)

    自动化测试中寻找元素并进行操作,如果在元素好找的情况下,相信大家都可以较熟练地编写用例脚本了,但光进行操作可能还不够,有时候也需要对预期结果进行判断. 这里介绍几个常用断言的使用方法,可以一定程度上帮 ...

  6. Zend Hash table 详解--转

    原文地址:http://www.phppan.com/2009/12/zend-hashtable/ 在PHP的Zend引擎中,有一个数据结构非常重要,它无处不在,是PHP数据存储的核心,各种常量.变 ...

  7. 【树链剖分换根】P3979 遥远的国度

    Description zcwwzdjn在追杀十分sb的zhx,而zhx逃入了一个遥远的国度.当zcwwzdjn准备进入遥远的国度继续追杀时,守护神RapiD阻拦了zcwwzdjn的去路,他需要zcw ...

  8. css美化Div边框的样式实例

    很多时候如果不是用了很多样式,很难把边框修饰得好看,看了一篇博文,觉得真的挺漂亮,也挺好看. 转载的博文地址 将这段美化的css代码 border:1px solid #96c2f1;backgrou ...

  9. 对于redis框架的理解(二)

    之前梳理过redis main函数主体流程 大体是 initServerConfig() -> loadServerConfig() -> daemonize() -> initSe ...

  10. stout代码分析之一:Duration类

    Duration类用于表示时间长度,可精确到纳秒. 代码实现在duration.hpp中,测试代码:duration_tests.cpp 相关api如下: parse, 将字符串转化成Duration ...