lc 343 Integer Break


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的更多相关文章

  1. [LeetCode] 343. Integer Break 整数拆分

    Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...

  2. 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 ...

  3. leetcode 343. Integer Break(dp或数学推导)

    Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...

  4. Leetcode 343. Integer Break

    Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...

  5. #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 ...

  6. 【LeetCode】343. Integer Break 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学解法 动态规划 日期 题目地址:https:// ...

  7. LeetCode题解 343.Integer Break

    题目:Given a positive integer n, break it into the sum of at least two positive integers and maximize ...

  8. (dp)343. Integer Break

    Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...

  9. 343. Integer Break

    Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...

随机推荐

  1. Handlebars.js 中文文档

    Home  »  前端   »   Handlebars.js 中文文档 Handlebars.js 中文文档 Posted in 前端 By KeenWon On 2014年4月3日 Views:  ...

  2. lmhostid获取hostid为空问题

    lmhostid获取hostid为空问题 问题描写叙述 今天迁移曾经的一个装有flexlm的虚拟机,结果发如今迁移后启动时报错 ... Wrong hostid on SERVER line for ...

  3. hashable

    Glossary — Python 3.6.5 documentation https://docs.python.org/3/glossary.html?highlight=equal hashab ...

  4. [QT开发小结]LNK1104: cannot open file ‘gdi32.lib’ 解决方法

    1.环境变量 : Path = ;C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\Bin; 添加变量: INCLUDE = C:\Program ...

  5. Masonry remake更新约束

    前言 说到iOS自动布局,有很多的解决办法.有的人使用xib/storyboard自动布局,也有人使用frame来适配.对于前者,笔者并不喜欢,也不支持.对于后者,更是麻烦,到处计算高度.宽度等,千万 ...

  6. FMDB数据库队列

    一.代码示例 1.需要先导入FMDB框架和头文件,由于该框架依赖于libsqlite库,所以还应该导入该库. 2.代码如下: 1 // 2 // YYViewController.m 3 // 05- ...

  7. BZOJ4259:残缺的字符串(FFT与字符串匹配)

    很久很久以前,在你刚刚学习字符串匹配的时候,有两个仅包含小写字母的字符串A和B,其中A串长度为m,B串长度为n.可当你现在再次碰到这两个串时,这两个串已经老化了,每个串都有不同程度的残缺. 你想对这两 ...

  8. 从free到page cache

    Free 我们经常用free查看服务器的内存使用情况,而free中的输出却有些让人困惑,如下:   图1-1 先看看各个数字的意义以及如何计算得到: free命令输出的第二行(Mem):这行分别显示了 ...

  9. ubuntu搭建开发环境踩坑实录

    谨以此文,记录和ubuntu系统不死不休的搏斗过程,后续待补. 1.双系统安装,windows采用uefi模式安装(优启通可制作uefi的win10安装盘),ubuntu不要划分boot区,而应该划分 ...

  10. UVa 11401 Triangle Counting (计数DP)

    题意:给定一个数 n,从1-n这些数中任意挑出3个数,能组成三角形的数目. 析:dp[i] 表示从1-i 个中任意挑出3个数,能组成三角形的数目. 代码如下: #pragma comment(link ...