LN : leetcode 343 Integer Break
lc 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 and not larger than 58.
DP Accepted
dp[i]代表n为i时,和之积的最大值,所以dp[n]即为答案。动态转移方程为dp[i] = max(j*dp[i-j],max(dp[i],j*(i-j))),对于i之前的每一个j都遍历,用j来确定是否就此划分,如果不划分则是dp[i],如果划分则是j*dp[i-j]或j*(i-j),三者取最大值即可。
class Solution {
public:
int integerBreak(int n) {
vector<int> dp(n+1, 1);
for (int i = 3; i <= n; i++) {
for (int j = 1; j < i; j++) {
dp[i] = max(j*dp[i-j],max(dp[i],j*(i-j)));
}
}
return dp[n];
}
};
LN : leetcode 343 Integer Break的更多相关文章
- [LeetCode] 343. Integer Break 整数拆分
Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...
- leetcode@ [343] Integer Break (Math & Dynamic Programming)
https://leetcode.com/problems/integer-break/ Given a positive integer n, break it into the sum of at ...
- 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 343. Integer Break
Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...
- #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 ...
- 【LeetCode】343. Integer Break 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学解法 动态规划 日期 题目地址:https:// ...
- LeetCode题解 343.Integer Break
题目:Given a positive integer n, break it into the sum of at least two positive integers and maximize ...
- (dp)343. Integer Break
Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...
- 343. Integer Break
Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...
随机推荐
- Android第一个个人APP(帐号助手)
第一个app上线了,关于帐号保存的一个app.本地保存,无须联网. 下载地址为:http://android.myapp.com/myapp/detail.htm?apkName=com.weeky. ...
- 高效5步走,高速搭建Hadoop2伪分布环境
前两天将Hadoop2的全然分布式搭建文档整理公布于网上(http://blog.csdn.net/aaronhadoop/article/details/24859369).朋友相邀.就再将Hado ...
- CSS制作翻牌特效
应一个朋友要求替他把原本静态页面做成翻牌的特效. 主要应用了CSS3的transform,transiton.首先写好标签,一个ul下两个li元素,通过position的absolue设置两个li元素 ...
- LiveWriter插入高亮代码插件介绍 基于SyntaxHighighter
Codeint main() { int i; printf("%d",i); } 插件介绍 辛苦了两人小时写日志不小心浏览器崩溃了,发誓以后一定记得用Word先写好. 将Word ...
- make运行阶段划分
1 make执行分为两个阶段 第一个阶段:读makefile并且建树阶段 第二个阶段:构建目标阶段 2 扩展的立即和推迟 在第一个阶段的扩展是立即,在第二个阶段或者在需要的时候再扩展是推迟,这里的需要 ...
- CSS 对齐方式
居中设置 Center Align - Using margin Setting the width of a block-level element will prevent it from str ...
- JavaScript基础简要
JavaScript 引用外部js : <script src="2.js"type="text/javascript"></scri ...
- Magic Grid ComboBox JQuery 版
在MagicCombo组件中嵌入Grid,以支持分页查找和跨页选取 1. 2. [代码][JavaScript]单选示例代码 <script type="text/jav ...
- ionic 和cordova的区别是什么
很多新朋友ionic基础教程都学完了,还是不知道ionic 和cordova 是什么关系 ionic是什么: Ionic(ionicframework)一款开源的Html5移动App开发框架,是Ang ...
- 整型变量修饰符,char类型数据存储原理,字节数,
//------------------整型变量修饰符 修饰符(int short long longlong signed unsigned)所有修饰符都是用来修整形 int 4short %hd ...