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.

Hint:

  1. There is a simple O(n) solution to this problem.
  2. You may check the breaking results of n ranging from 7 to 10 to discover the regularities.

题意:给一个n(n>=2),求相加等于n且乘积最大的一组整数的积。

题解

这其实就是一道高中数学题,但是leetcode上的数据比较水,导致完全不用数学推导的O(n2)的dp也可以过。

解法一(纯dp):

令dp[n]为n对应的最大积。

那么递推方程就是:dp[n]=max(i*dp[n-i],i*(n-i))(其中i从1到n-1)。

边界:dp[2]=1;

时间复杂度:O(n2)

class Solution {
public:
int integerBreak(int n) {
int dp[n];
dp[]=;
dp[]=;
for(int i=;i<=n;i++){
dp[i]=-;
for(int j=;j<i;j++){
dp[i]=max(j*dp[i-j],max(dp[i],j*(i-j)));
}
}
return dp[n];
}
};

解法二(当成数学题来做就好):

由均值不等式(n个数的算术平均数大于等于它们的几何平均数):

    >=   

得:当把输入的n拆分成几个相等的数时它们的积最大。

那么问题来了,拆分成几个呢?

为了方便使用导数,我们先假设我们可以把n拆分成实数。那么设每一个数为x,则一共有n/x个数。

设它们的积为f(x),则f(x)=x(n/x),那么怎么求f(x)最大值呢?求导数!

f′(x)=(n/x2)  *  x(n/x)  * (1-lnx)

当x=e时取极大值。

而我们题目里规定x为整数,那么我们只需要取的x越靠近e越好。那么2<e<3,而且e=2.71828...,所以取3是最好的,如果取不到3就取2。

幂运算复杂度为O(lgn),所以这个算法复杂度为O(lgn)。

class Solution {
public:
int integerBreak(int n) {
if(n == )
return ;
else if(n == )
return ;
else if(n% == )
return pow(, n/);
else if(n% == )
return * * pow(, (n - ) / );
else
return * pow(, n/);
}
};

leetcode 343. Integer Break(dp或数学推导)的更多相关文章

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

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

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

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

  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. [LeetCode]Integer Break(Dp或胡搞或推公式)

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

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

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

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

  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. LeetCode题解 343.Integer Break

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

随机推荐

  1. (二)MySQL学习笔记

    1.视图 视图是一系列select语句返回的可视化结果集,是一张虚拟表.更多介绍请查看http://tool.oschina.net/apidocs/apidoc?api=mysql-5.1-zh 视 ...

  2. JavaScript中:表达式和语句的区别

    JavaScript中:表达式和语句的区别 Javascript语言精粹:表达式是由运算符构成,并运算产生结果的语法结构.程序是由语句构成,语句则是由“:(分号)”分隔的句子或命令.如果在表达式后面加 ...

  3. [牛客Wannafly挑战赛27D]绿魔法师

    description newcoder 给你一个空的可重集合\(S\). \(n\)次操作,每次操作给出\(x\),\(k\),\(p\),执行以下操作: \(opt\ 1\):在S中加入x. \( ...

  4. BZOJ2659 [Beijing wc2012]算不出的算式 【数形结合】

    题目链接 BZOJ2659 题解 真没想到,, 观察式子 \[\sum\limits_{k = 1}^{\frac{p - 1}{2}} \lfloor \frac{kq}{p} \rfloor\] ...

  5. BZOJ1832 聚会

    Description:Y岛风景美丽宜人,气候温和,物产丰富.Y岛上有N个城市,有N-1条城市间的道路连接着它们.每一条道路都连接某两个城市.幸运的是,小可可通过这些道路可以走遍Y岛的所有城市.神奇的 ...

  6. 覆盖的面积 HDU - 1255 (线段树-扫描线)模板提

    给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积. Input输入数据的第一行是一个正整数T(1<=T<=100),代表测试数据的数量.每个测试数据的第一行是一个正整数N(1& ...

  7. uboot启动原理

    1.裸机运行程序时一般情况下程序代码小于16KB将其下载地址设置到BL1的起始地址.BL0会自动加载并执行BL1. 当程序大于16kB时无法直接运行. 例如UBOOT就大于16KB,执行的原理为.将程 ...

  8. Android核心类源码分析

    Handler流程1.首先Looper.prepare()在本线程中保存一个Looper实例,然后该实例中保存一个MessageQueue对象:因为Looper.prepare()在一个线程中只能调用 ...

  9. CSS3 :empty 选择器

    这可是个好东西,我也是这个星期才发现的,下面我们来说具体功能. <!DOCTYPE html> <html> <head> <meta charset=&qu ...

  10. 温习js中对象的继承

    温故而知新 XD 1. 关于原型和构造函数的几个知识要点: 使用new 操作符调用构造函数,会经历以下四个步骤: 1.1. 创建一个新对象: 1.2. 将构造函数的作用域赋给新对象(因此 this 就 ...