(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.
public class Solution { //dp
public int integerBreak(int n) {
if(n==2) return 1;
if(n==3) return 2;
int[] dp=new int[n+1];
dp[0]=dp[1]=1;
dp[2]=2;dp[3]=3;
for(int i=4;i<=n;i++){
dp[i]=dp[1]*dp[i-1];
for(int j=2;j<=i/2;j++){
dp[i]=(dp[i]>dp[j]*dp[i-j])?dp[i]:dp[j]*dp[i-j];
}
}
return dp[n];
}
}
(dp)343. Integer Break的更多相关文章
- LightOJ 1033 Generating Palindromes(dp)
LightOJ 1033 Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...
- lightOJ 1047 Neighbor House (DP)
lightOJ 1047 Neighbor House (DP) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730# ...
- 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之动态规划(DP)专题-746. 使用最小花费爬楼梯(Min Cost Climbing Stairs)
Leetcode之动态规划(DP)专题-746. 使用最小花费爬楼梯(Min Cost Climbing Stairs) 数组的每个索引做为一个阶梯,第 i个阶梯对应着一个非负数的体力花费值 cost ...
- Leetcode之动态规划(DP)专题-931. 下降路径最小和(Minimum Falling Path Sum)
Leetcode之动态规划(DP)专题-931. 下降路径最小和(Minimum Falling Path Sum) 给定一个方形整数数组 A,我们想要得到通过 A 的下降路径的最小和. 下降路径可以 ...
- Leetcode之动态规划(DP)专题-53. 最大子序和(Maximum Subarray)
Leetcode之动态规划(DP)专题-53. 最大子序和(Maximum Subarray) 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. ...
- Leetcode之动态规划(DP)专题-1025. 除数博弈(Divisor Game)
Leetcode之动态规划(DP)专题-1025. 除数博弈(Divisor Game) 爱丽丝和鲍勃一起玩游戏,他们轮流行动.爱丽丝先手开局. 最初,黑板上有一个数字 N .在每个玩家的回合,玩家需 ...
- Leetcode之动态规划(DP)专题-338. 比特位计数(Counting Bits)
Leetcode之动态规划(DP)专题-338. 比特位计数(Counting Bits) 给定一个非负整数 num.对于 0 ≤ i ≤ num 范围中的每个数字 i ,计算其二进制数中的 1 的数 ...
- #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 ...
随机推荐
- CLR via C# 3rd - 01 - The CLR's Execution Model
1. Assemly A managed module is a standard 32-bit Microsoft Windoes portable executable (PE32) ...
- yii 初步安装
第一步: window下点击>开始 >运行CMD命令. 第二步:进入Yiic文件的目录 (例如在D盘里面 D:/yii/framework) 第三步:D:\yii\framework& ...
- R语言实现 广义加性模型 Generalized Additive Models(GAM) 入门
转载请说明. R语言官网:http://www.r-project.org/ R语言软件下载:http://ftp.ctex.org/mirrors/CRAN/ 注:下载时点击 ins ...
- EL与Velocity基本语法总结:
El(expression language): 基本语法点: $与{}搭配使用是常态取值 . 与[]的区别,后者可以取特殊值:- .等 支持一些基本的逻辑运算: && || > ...
- Android常用库
原文链接:http://www.jianshu.com/p/19368c2cdcaf 系统框架 1. 网络请求 Android Async HTTP Android异步HTTP库 AndroidAsy ...
- Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- ✡ leetcode 174. Dungeon Game 地牢游戏 --------- java
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...
- 关于js中空值比较和传值的问题
昨天写导出功能时,有个条件审核状态,其中一个审核状态的key为0,我也是醉了. 然后我注意到这方面的问题,在网上找了找,我的理解可能有点问题.但是目前也就是这样了,以后在学习吧! 正文: js中各种类 ...
- mysql coalesce函数
COALESCE函数从值列表中返回第一个非NULL的值,当遇到NULL值时将其替换为0. coalesce(str1,str2....); e.g. 需要在表中查出所有比'WARD'提成(COMM ...
- [leetcode] 根据String数组构造TreeNode,用于LeetCode树结构相关的测试用例
LeetCode 跟树结构相关的题目的测试用例中大多是通过String数组来构造树.例如{2,#,3,#,4,#,5,#,6},可以构造出如下的树(将树结构逆时针选择90度显示): 6 ...