343. Integer Break

原创 2016年08月30日 12:32:21

题目

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.

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.
分析

	2 - 1,1
        3 - 1,2
        4 - 2,2
        5 - 3,2
        6 - 3,3
        7 - 3,4
        8 - 3,3,2
        9 - 3,3,3
        10 - 3,3,4
        11 - 3,3,3,2
        12 - 3,3,3,3
        13 - 3,3,3,4
其中的数学思想参见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 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.

class Solution {
public:
    int integerBreak(int n) {
        if (n==2)
            return 1;
        if (n==3)
            return 2;
        if (n%3 == 0)
        {
            return pow(3,n/3);
        }
        if (n%3 == 2)
        {
            return pow(3,n/3)*2;
        }
        return pow(3,(n/3)-1)*4;
        
    }
};



相关文章推荐

343. Integer Break 等

343. Integer Break 原题: Given a positive integer n, break it into the sum of at least two positi...
  • Mr_Zing
  • Mr_Zing
  • 2016年10月23日 11:26
  • 259

[LC343] Integer Break

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

[LeetCode]problem 343. Integer Break

TAG分解n得到长度至少为2、和为n的数字序列,使其乘积最大的方法——一直从中分出3。数学 题目链接方法懵逼。按照提示找规律,然而也没有找到。果然智障。看了DISCUZZWhy factor 2 or...

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

拆分正整数成多个数相加,得出最大乘积

Leetcode 343. Integer Break

原题链接: 343. Integer Break 343. Integer Break    My Submissions Question Editorial Solution T...

<LeetCode OJ> 343. Integer Break

Total Accepted: 2481 Total Submissions: 5881 Difficulty: Medium Given a positive integer n, bre...

LeetCode #343 - Integer Break - Medium

ProblemGiven a positive integer n, break it into the sum of at least two positive integers and maxim...
  • Arcome
  • Arcome
  • 2016年11月02日 08:49
  • 146

leetcode 343. Integer Break

今天开始抽空把我在leetcode上 刷到的一些题及思路搬到这里来。我是半吊子水平,所以此举绝不是炫耀,主要目的是对自己做过的东西做个记录,当然如果能对任何人有所启发,那就太荣幸了。OK少说废话了,说...

LeetCode解题报告 343. Integer Break [medium]

题目描述 Given a positive integer n, break it into the sum of at least two positive integers and maximi...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:343. Integer Break
举报原因:
原因补充:

(最多只允许输入30个字)