leetcode343. Integer Break

最近leetcode好像总是被墙什么鬼哦,不管,发现一道很好玩的题目: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.

题目大意就是说你将一个整数分解成至少两个以上的较小的数的和,使得他的乘积最大。最开始的时候呢,很容易想到肯定是分解成多个相同的数(最后一个带上余数),但是接下去就不是很好想了,不过这个数应该是1<x<10 hhh,不过没有科学的证明,看了评论区的讲解后恍然大悟,感慨数学的神奇,以下是评论区的原文

Why factor 2 or 3? The math behind this problem.

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 x^(n/x), and we want to maximize it.

Taking its derivative gives us n * x^(n/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.

大概翻译过来的意思思我们将n分解为n/x个x,则乘积为 x^(n/x),(余数不是重点),对它进行求导可以得到n * x^(n/x-2) * (1 - ln(x)).这个函数呢,在 x = e 的时候达到了最大值(导数为0),那么显然x取2或3会是乘积最大的,后面也可以证明当这个数足够大的时候,取3的结果会是好一点的。

如下:

f(n) = f(n - 3) * 3 = f(n - 6) * 3 * 3 = f(n - 6) * 9
f(n) = f(n - 2) * 2 = f(n - 4) * 2 * 2 = f(n - 6) * 2 * 2 * 2 =f(n - 6) * 8
Therefore, when n is sufficiently large, 3 is always preferred.

代码

class Solution {
public:
    int integerBreak(int n) {
        if(n == 2 || n == 3)
            return n-1;
        int res = 1;
        while(n > 4) {
            n -= 3;
            res *= 3;
        }
        return n * res;
    }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值