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.

很明显又是最优问题。

Dp的做法需要先枚举前5个值。

然后判断一下 max(*3,2*2)就好了。

不过这里用胡搞的方法更快!!!!!

(随便比较脏。。但是O(1)呀。。。。。。逃)

public class Solution {

    public int integerBreak( int n ) {
if(n<5){
if(n==1) return 0;
else if(n==2) return 1;
else if(n==3) return 2;
else{
return 4;
}
}
else{
int k = n/3;
int m = n%3;
Long ret = 1L;
ret = ( long )Math.pow( 3, k );
if(m==1){
ret=(ret / 3) * 4;
}else if(m==2){
ret*=m;
}
return ret.intValue();
}
} }

[LeetCode]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 整数拆分

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

  3. LeetCode "Integer Break"

    A typical CS style DP based solution: class Solution(object): def __init__(self): self.hm = {} def i ...

  4. LeetCode——Integer Break

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

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

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

  6. 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 ...

  7. LeetCode 343. 整数拆分(Integer Break) 25

    343. 整数拆分 343. Integer Break 题目描述 给定一个正整数 n,将其拆分为至少两个正整数的和,并使这些整数的乘积最大化. 返回你可以获得的最大乘积. 每日一算法2019/5/2 ...

  8. #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 ...

  9. HDU 4690 EBCDIC (2013多校 1005题 胡搞题)

    EBCDIC Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)Total Su ...

随机推荐

  1. 使用原始XML资源——定义原始XML资源

    原始XML资源一般保存在/res/xml路径下——当使用ADT创建Android应用时,/res/目录下并没有包含该目录,开发者应该自行手动创建xml目录. 接下来Android应用对原始XML资源没 ...

  2. 使用(Drawable)资源———AnimationDrawable资源

    AnimationDrawable代表一个动画. 下面以补间动画为例来介绍如何定义AnimationDrawable资源,定义补间动画的XML资源文件已<set.../>元素作为根元素,该 ...

  3. Delphi TRect函数例子

    {   在网上看到个这个例子感觉还不错,将它移到自己的博客里没事的时候看看:   TRect    作用:保存一个矩形的左上角和右下角的坐标值:      声明:       type TRect = ...

  4. SQL Server @@ERROR 用法

    @@error是系统函数,当没有发生错误时返回0,如果发生错误时@@error<>0,并返回错误号,每个SQL语句执行完,@@error值都会变. @@error只记录当前错误,如果存储过 ...

  5. jquery 全选 全不选 事件绑定

    <td width="82%" colspan="3"><input type="checkbox" id="a ...

  6. es6笔记7^_^class

    ES6提供了更接近传统语言的写法,引入了Class(类)这个概念,作为对象的模板.通过class关键字,可以定义类. 部分来自JavaScript ES6 class指南.mozilla https: ...

  7. CodeForces 451B

    Sort the Array Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Sub ...

  8. Redis系列一(Redis环境的搭建)

    最近工作中需要Redis缓存,由于也是第一次弄,在网上找了很多资料,在此记录一下. 安装Redis 我使用的系统是CentOS 6.6,安装步骤如下: 1.下载源码,解压后编译源码.(如果没有 wge ...

  9. SuperSocket入门(五)-常用协议实现模版及FixedSizeReceiveFilter示例

             Socket里面的协议解析是Socket通讯程序设计中最复杂的地方,如果你的应用层协议设计或实现不佳,Socket通讯中常见的粘包,分包就难以避免.SuperSocket内置了命令行 ...

  10. TypeScript教程2

    在TS中,我们允许开发人员使用面向对象技术. 1.类让我们看看一个简单的基于类的例子: class Greeter { greeting: string; constructor(message: s ...