(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 ...
随机推荐
- FreeBSD 配置
FreeBSD 配置 1. FreeBSD源代码下载站点:
- 提升Nginx+PHP-FPM性能技巧
/etc/php-fpm.d 2.1进程数 php-fpm初始/空闲/最大worker进程数 pm.max_children = 300 pm.start_servers = ...
- python的编码判断_unicode_gbk/gb2312_utf8(附函数)
python中, 我们平常使用最多的三种编码为 gbk/gb2312, utf8 , unicode. 而python中并没有一个函数来进行 编码的判断.今天,主要对这三种编码进行讨论,并给出区分 ...
- linux下JDK1.7安装
http://mnt.conf.blog.163.com/blog/static/115668258201210793915876/ 一.软件下载1.下载JDK(下面分别是32位系统和64位系统下的版 ...
- margin负值的几种妙用
1:定位+margin负值实现元素水平垂直居中 div{ position: absolute; z-index: 1; left: 50%; margin-left: -50px; width: 1 ...
- js的事件委托
什么是事件委托:通俗的讲,事件就是onclick,onmouseover,onmouseout,等就是事件,委托呢,就是让别人来做,这个事件本来是加在某些元素上的,然而你却加到别人身上来做,完成这个事 ...
- jsp和servlet中文乱码
jsp和servlet之间出现中文乱码的集中原因和解决方法详解:http://blog.csdn.net/longyuhome/article/details/7856270
- Mongodb和Hive详细对比
本文主要用于分析在大数据场景下Mongodb和Hive的优缺点: 支持的数据类型 支持的查询 支持的数据量 性能优化手段
- sql 查看数据库物理文件路径
方式一:用于查询all数据库,适用于SQL2005及以上版本. USE [Master] GO /****** 对象 : Table [dbo].[Master] 脚本日期 : 06/29/2 ...
- ✡ leetcode 174. Dungeon Game 地牢游戏 --------- java
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...