leetcode 343. Integer Break(dp或数学推导)
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:
- There is a simple O(n) solution to this problem.
- 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或数学推导)的更多相关文章
- 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 ...
- [LeetCode] 343. Integer Break 整数拆分
Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...
- 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
Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...
- [LeetCode]Integer Break(Dp或胡搞或推公式)
343. Integer Break Given a positive integer n, break it into the sum of at least two positive intege ...
- #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 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学解法 动态规划 日期 题目地址:https:// ...
- (dp)343. Integer Break
Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...
- LeetCode题解 343.Integer Break
题目:Given a positive integer n, break it into the sum of at least two positive integers and maximize ...
随机推荐
- 单选 name的值相同时候 就会产生互斥现象
- BZOJ4889 & 洛谷3759:[TJOI2017]不勤劳的图书管理员——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=4889 https://www.luogu.org/problemnew/show/P3759 加里 ...
- BZOJ4573:[ZJOI2016]大森林——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=4573 https://www.luogu.org/problemnew/show/P3348#sub ...
- NOIP2017金秋冲刺训练营杯联赛模拟大奖赛第二轮Day2题解
肝了两题... T1一眼题,分解质因数,找出2的个数和5的个数取min输出 #include<iostream> #include<cstring> #include<c ...
- 剑桥offer(41~50)
41.题目描述 求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). class Solution { pub ...
- Codeforces Round #326 (Div. 2) B Duff in Love 简单数论 姿势涨
B. Duff in Love time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- springboot整合thumbnailator实现图片压缩
springboot整合thumbnailator实现图片压缩 前言 最近由于首页产品列表图片显示太慢,经过研究发现是用户上传的图片太大. 针对这个问题,想到的解决方案是: 1. 产品上传时,限定图片 ...
- tcp/ip 学习-通过视频学习
视频下载地址:http://down.51cto.com/zt/5518/ http://www.icoolxue.com/album/show/328 每天可以拿两个番茄钟看视频,主要目的还是了解, ...
- 51Nod 1090 3个数之和
Input示例 7 -3 -2 -1 0 1 2 3 Output示例 -3 0 3 -3 1 2 -2 -1 3 -2 0 2 -1 0 1 #include "bits/stdc++.h ...
- LightOJ 1319 - Monkey Tradition CRT除数互质版
本题亦是非常裸的CRT. CRT的余数方程 那么定义 则 其中 为模mi的逆元. /** @Date : 2016-10-23-15.11 * @Author : Lweleth (SoungEarl ...