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. js原生设计模式——7原型模式之new+call(this)组合应用再探讨实例

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...

  2. iOS 之 UICollectionView

    1. iOS 之 UICollectionView 之 原理介绍 2. iOS 之 UICollectionView 之 开发步骤 之 OC 3. iOS 之 UICollectionView 之 开 ...

  3. 在ubuntu下增加root用户并登录

    1.打开终端. 2.输入sudo gedit /usr/share/lightdm/lightdm.conf.d/50-ubuntu.conf 3.在弹出的编辑框里输入:greeter-show-ma ...

  4. VoiceEngine获取硬件信息

    #include "webrtc\voice_engine\include\voe_base.h" #include "webrtc\voice_engine\inclu ...

  5. use strict 的优点

    JavaScript严格模式详解 http://blog.csdn.net/airingyuan/article/details/25036297 http://ourjs.com/detail/52 ...

  6. 《转》VS2010进行远程调试方法总结

    ---恢复内容开始--- 原文地址:http://blog.sina.com.cn/s/blog_a459dcf5010153o7.html 假设现在有A.B两台PC机. VS2010安装在A机器上, ...

  7. My Vim配置

    set cindent " 使用 C/C++ 语言的自动缩进方式" set cinoptions={,1s,t0,n-,p2s,(03s,=.5s,>1s,=1s,:1s & ...

  8. CodeForces 512B(区间dp)

    D - Fox And Jumping Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64 ...

  9. Spring之循环依赖

    转:http://my.oschina.net/tryUcatchUfinallyU/blog/287936 概述 如何检测循环依赖 循环依赖如何解决 Spring如何解决循环依赖 主要的几个缓存 主 ...

  10. [html5] 学习笔记- html拖放

    拖放是一种常见的特性,即抓取对象以后拖到另一个位置,在HTML5中,拖放是标准的一部分,任何元素都能够拖放. 1.html5拖放:(drag和drop)是HTML5标准的组成部分 拖动开始:ondra ...