LeetCode——Integer Break
Question
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.
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
Solution
动态规划,自底向上求解可以划分方式的最大值,注意要考虑不划分的情况,所以要取两者的最大值。
Code
class Solution {
public:
int integerBreak(int n) {
table.resize(60);
table[1] = 1;
for (int i = 2; i <= n; i++) {
table[i] = 1;
for (int j = 1; j <= i / 2; j++) {
// 要考虑不划分的情况
int tmp = max(j, table[j]) * max(i - j, table[i - j]);
if (tmp > table[i])
table[i] = tmp;
}
}
return table[n];
}
private:
vector<int> table;
};
LeetCode——Integer Break的更多相关文章
- [LeetCode]Integer Break(Dp或胡搞或推公式)
343. Integer Break Given a positive integer n, break it into the sum of at least two positive intege ...
- [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】343. Integer Break 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学解法 动态规划 日期 题目地址:https:// ...
- LeetCode 343. 整数拆分(Integer Break) 25
343. 整数拆分 343. Integer Break 题目描述 给定一个正整数 n,将其拆分为至少两个正整数的和,并使这些整数的乘积最大化. 返回你可以获得的最大乘积. 每日一算法2019/5/2 ...
- 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 ...
- #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 (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 ...
随机推荐
- Educational Codeforces Round 25 E. Minimal Labels&&hdu1258
这两道题都需要用到拓扑排序,所以先介绍一下什么叫做拓扑排序. 这里说一下我是怎么理解的,拓扑排序实在DAG中进行的,根据图中的有向边的方向决定大小关系,具体可以下面的题目中理解其含义 Educatio ...
- PMP私有广告交易市场
[资源]互联网广告新知:半小时读懂PMP私有广告交易市场是什么? https://socialbeta.com/t/resource-what-is-pmp.html SocialBeta | 201 ...
- C#、devExpress 的 给bandedGrid加菜单功能 :复制、粘贴的例子(转)
C#.devExpress 的 给bandedGrid加菜单功能 :复制.粘贴的例子 CopyFromGrid PasteToGrid PasteNewRowsToGrid private void ...
- 并发编程&数据库 - 考核题
第八章主要内容 第八章:线程.进程.队列.IO多路模型 操作系统工作原理介绍.线程.进程演化史.特点.区别.互斥锁.信号.事件.join.GIL.进程间通信.管道.队列. 生产者消息者模型.异步模型. ...
- SmokePing安装手册
SmokePing安装部署 SmokePing简介 Smokeping是一款用于网络性能监测的开源监控软件,主要用于对IDC的网络状况,网络质量,稳定性等做检测,通过rrdtool制图方式,图形化地展 ...
- cordova 入门
1. npm install -g cordova On Windows, npm can usually be found at C:\Users\username\AppData\Roaming\ ...
- docker部署oracle
oracle部署 创建oracle用户 [root@stpass-15 ~]# useradd oracle[root@stpass-15 oracle]# cd oracle [root@stpas ...
- 基于.net core2.1开发遇到的问题记录以及解决方案
问题1:升级EFCore 到2.1一直报'Void Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommandBuilderFac ...
- [golang note] 包和导入
package的作用 √ package是golang最基本的分发单位和工程管理中依赖关系的体现. √ 每个golang源代码文件开头都拥有一个package声明,表示该g ...
- django 【认证】
一.验证 1.views.py from django.contrib.auth.decorators import login_required from django.contrib.auth i ...