[LeetCode]Integer Break(Dp或胡搞或推公式)
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或胡搞或推公式)的更多相关文章
- leetcode 343. Integer Break(dp或数学推导)
Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...
- [LeetCode] Integer Break 整数拆分
Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...
- LeetCode "Integer Break"
A typical CS style DP based solution: class Solution(object): def __init__(self): self.hm = {} def i ...
- LeetCode——Integer Break
Question Given a positive integer n, break it into the sum of at least two positive integers and max ...
- 【LeetCode】343. Integer Break 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学解法 动态规划 日期 题目地址:https:// ...
- 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 ...
- LeetCode 343. 整数拆分(Integer Break) 25
343. 整数拆分 343. Integer Break 题目描述 给定一个正整数 n,将其拆分为至少两个正整数的和,并使这些整数的乘积最大化. 返回你可以获得的最大乘积. 每日一算法2019/5/2 ...
- #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 ...
- HDU 4690 EBCDIC (2013多校 1005题 胡搞题)
EBCDIC Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)Total Su ...
随机推荐
- jpg图片在开发板上显示
文件IO项目: 在开发板屏幕上循环显示目录里的图片 a.按照一定的间隔循环显示目录里的bmp图片 b.实现手指滑动来显示目录里的图片(bmp,jpg)上一张,下一张 d1: 1.能操控屏幕(查询开发板 ...
- package.json 里 devDependencies和dependencies的区别
我们在使用npm install 安装模块或插件的时候,有两种命令把他们写入到 package.json 文件里面去,比如: --save-dev --save 在 package.json 文件里面 ...
- Python中执行系统命令常见的几种方法--转载
Python中执行系统命令常见的几种方法 Python中执行系统命令常见的几种方法有: (1)os.system # 仅仅在一个子终端运行系统命令,而不能获取命令执行后的返回信息 # 如果再命令行下执 ...
- mysql5.5慢日志设置和查询
mysql> showvariables like '%version%'; +-------------------------+---------------------+ | Variab ...
- lufylegend库 LGraphics扭曲图片
lufylegend库 LGraphics扭曲图片 <!DOCTYPE html> <html lang="en"> <head> <me ...
- Nancy简单实战之NancyMusicStore(四):实现购物车
前言 上一篇,我们完成了商品的详情和商品的管理,这一篇我们来完成最后的一个购物车功能. 购物车,不外乎这几个功能:添加商品到购物车,删除购物车中的商品,对购物车中的商品进行结算. MVC MusicS ...
- jq返回顶部
今天发工资了,哎,更加坚定我要努力的学习,没资本,只能玩技术了.人呢,想的的开,才行,虽然有些不甘心,不过确实,现在的技术只值这个 价格.不过做到问心无愧就够了,不然人之贪婪,真的收也收不回.好了,今 ...
- HDU1166(分块)
敌兵布阵 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status ...
- WebForm 控件(一)、连接数据库
一.控件 [简单控件] (一)文字显示 1.Label → 在html中相当于span <asp:Label ID="控件名 runat="server" Tex ...
- 模拟java的split函数,分割字符串,类似于java的split方法
/*自定义oracle的分割函数*//*定义一个type,用户接收返回的数据集合类型*/create or replace type splitType as table of varchar2(40 ...