343. Integer Break
2016-04-30 22:05
316 查看
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.
分析:
动态规划:
#define max(a,b) (a>b)?a:b;
class Solution {
public:
int integerBreak(int n) {
vector<int> dp(n+1,0);
dp[1]=dp[0]=1;
for(int i=2;i<=n;++i)
for(int j=1;j<i;++j)
{int temp=max(i-j,dp[i-j]);dp[i]=max(dp[i],j*temp); }
return dp
;
}
};
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.
分析:
动态规划:
#define max(a,b) (a>b)?a:b;
class Solution {
public:
int integerBreak(int n) {
vector<int> dp(n+1,0);
dp[1]=dp[0]=1;
for(int i=2;i<=n;++i)
for(int j=1;j<i;++j)
{int temp=max(i-j,dp[i-j]);dp[i]=max(dp[i],j*temp); }
return dp
;
}
};
相关文章推荐
- iOS中实现获取文本内容的宽高
- 2.一个奇怪的fork程序
- MOOC的Python笔记(二)python数据类型与输入输出
- [iOS] KVC 和 KVO
- 素数问题
- windows exploer 电脑不停刷新闪屏
- javascript中Object.create与new的不同
- OpenCV学习之安装
- HTML5第9节课堂笔记(初探mui,制作手机归属地查询)
- ABP学习笔记
- Linux内核学习总结
- 学习PS基础
- 算法导论学习笔记(二)分治策略之最大子数组
- Light OJ 1031 Easy Game 区间DP
- 1.系统调用的过程
- Volley源码解析(一),基本概述
- am335x mux配置
- mvc小结
- Poj 2230 Watchcow【欧拉回路】
- uva1220树的最大独立集(2 )