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

  1. [LeetCode]Integer Break(Dp或胡搞或推公式)

    343. Integer Break Given a positive integer n, break it into the sum of at least two positive intege ...

  2. [LeetCode] Integer Break 整数拆分

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

  3. LeetCode "Integer Break"

    A typical CS style DP based solution: class Solution(object): def __init__(self): self.hm = {} def i ...

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

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

  5. LeetCode 343. 整数拆分(Integer Break) 25

    343. 整数拆分 343. Integer Break 题目描述 给定一个正整数 n,将其拆分为至少两个正整数的和,并使这些整数的乘积最大化. 返回你可以获得的最大乘积. 每日一算法2019/5/2 ...

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

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

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

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

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

随机推荐

  1. MapReduce分区和排序

    一.排序 排序: 需求:根据用户每月使用的流量按照使用的流量多少排序 接口-->WritableCompareable 排序操作在hadoop中属于默认的行为.默认按照字典殊勋排序. 排序的分类 ...

  2. python一两行代码完成的骚操作

    分享一个前几天晚上粉丝问的问题,觉得很实用的一个问题,用python读取Excel并保存字典,如何做? 下面是该同学问题截图和代码 ​ 代码截图是下面这样的 ​ 不知道大家第一眼看了这个代码,什么感受 ...

  3. C的指针疑惑:C和指针17(经典抽象数据类型)

    堆栈这种数据最鲜明的特点是:后进先出. 用动态数组实现堆栈: #include "C17.h" #include <stdio.h> #include <stdl ...

  4. appium ios 自动化测试

    iOS自动化测试:Appium 从入门到实践https://www.jianshu.com/p/43f858180557appium自动化测试iOS Demohttps://www.jianshu.c ...

  5. 一、html

    一.html相关概念 html是 htyper text markup language 即超文本标记语言,超文本就是指页面内可以包含图片.链接,甚至音乐.程序等非文字元素,而标记语言:即标记(标签) ...

  6. Writing a device driver for Windows

    Writing a device driver for Windows        In order to write a device driver for windows, one needs ...

  7. smart基础原理

    1html模板页面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://w ...

  8. hadoop streaming anaconda python 计算平均值

    原始Liunx 的python版本不带numpy ,安装了anaconda 之后,使用hadoop streaming 时无法调用anaconda python  , 后来发现是参数没设置好... 进 ...

  9. 性能调优之MySQL篇三:MySQL配置定位以及优化

    1.优化方式 一般的优化方法有:硬件优化,配置优化,sql优化,表结构优化.下面仅仅介绍配置优化,具体优化设置可以参考本人另外一篇博客,传送门:https://www.cnblogs.com/lang ...

  10. redis安全设置

    1. 设置监听ip为本地和内网ip bind 127.0.0.1 192.168.1.99 ## 可以是多个ip,用空格分割 2. 设置监听端口 port 16379 3. 设置密码 在配置文件中加入 ...