博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode 343. Integer Break
阅读量:5284 次
发布时间:2019-06-14

本文共 2783 字,大约阅读时间需要 9 分钟。

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.

分析

这道题用动态规划的话,比较简单

bool cmp(vector
v1,vector
v2){ return v1[1]!=v2[1]?v1[1]
>& pairs) { sort(pairs.begin(),pairs.end(),cmp); int dp[pairs.size()+1]={1}; int longest=1,len=1; for(int i=1;i
=0;j--) if(pairs[i][0]>pairs[j][1]){ dp[i]=max(dp[j]+1,dp[i]); if(longest

继续分析

看了牛人的解题与分析后:

I saw many solutions were referring to factors of 2 and 3. But why these two magic numbers? Why other factors do not work?
Let’s study the math behind it.

For convenience, say n is sufficiently large and can be broken into any smaller real positive numbers. We now try to calculate which real number generates the largest product.

Assume we break n into (n / x) x’s, then the product will be xn/x, and we want to maximize it.

Taking its derivative gives us n * xn/x-2 * (1 - ln(x)).

The derivative is positive when 0 < x < e, and equal to 0 when x = e, then becomes negative when x > e,
which indicates that the product increases as x increases, then reaches its maximum when x = e, then starts dropping.

This reveals the fact that if n is sufficiently large and we are allowed to break n into real numbers,

the best idea is to break it into nearly all e’s.
On the other hand, if n is sufficiently large and we can only break n into integers, we should choose integers that are closer to e.
The only potential candidates are 2 and 3 since 2 < e < 3, but we will generally prefer 3 to 2. Why?

Of course, one can prove it based on the formula above, but there is a more natural way shown as follows.

6 = 2 + 2 + 2 = 3 + 3. But 2 * 2 * 2 < 3 * 3.

Therefore, if there are three 2’s in the decomposition, we can replace them by two 3’s to gain a larger product.

All the analysis above assumes n is significantly large. When n is small (say n <= 10), it may contain flaws.

For instance, when n = 4, we have 2 * 2 > 3 * 1.
To fix it, we keep breaking n into 3’s until n gets smaller than 10, then solve the problem by brute-force.
好有道理有没有,多用3和2

class Solution {public:    int integerBreak(int n) {        if(n <= 3)            return n - 1;        int cnt = 1;        while(n > 2) {            cnt = cnt * 3;            n = n - 3;        }        if(n == 0)             return cnt;        else if(n == 1)            return cnt / 3 * 4;        else             return cnt * 2;    }};

转载于:https://www.cnblogs.com/A-Little-Nut/p/8439101.html

你可能感兴趣的文章
sqlserver 行转列、列转行[转]
查看>>
【IScroll深入学习】解决IScroll疑难杂症
查看>>
python 数据类型
查看>>
108-PHP类成员protected和private成员属性不能被查看数值
查看>>
css控制height充满浏览器视口
查看>>
Linux 系统目录结构
查看>>
python学习之 - XML
查看>>
css问题小计
查看>>
Laravel学习笔记(三)数据库 数据库迁移
查看>>
ORACLE查看并修改最大连接数
查看>>
box-flex不均分问题
查看>>
Python--GIL 详解
查看>>
Oracle数据导入Mysql中
查看>>
BZOJ-4424 &&CodeForces-19E Fairy DP+dfs (Link-Cut-Tree可A)
查看>>
MongoDB学习笔记——聚合操作之group,distinct,count
查看>>
大道至简读后感(第四章)
查看>>
IDA IDC Tutorials: Additional Auto-Commenting
查看>>
k8s-存储卷1-十二
查看>>
在Android中Intent的概念及应用(二)——Intent过滤器相关选项
查看>>
第十六章 多态性(一)
查看>>