343. Integer Break

Source: Internet
Author: User

Problem

Divides a positive integer into the sum of at least two positive integers, which maximizes the product of these integers and gives the maximum product.

Input:10
Output:36
Explanation:10 = 3 + 3 + 4, 3x3x4 = 36

Ideas

When PQ > P + q, as long as p and Q are greater than 1 satisfied. In a positive integer, two is 2 o'clock pq==p+q, so as long as a minimum of 2, the other is at least 3, that is, p+q at least 5, then as long as a number greater than or equal to 5, you can divide this number into p+q, so that PQ > P + q, if p or Q there are more than or equal to 5 of the number, The P or Q continues to decompose, and continues to decompose until the factor is less than 5.

Because to decompose to less than 5, the final factor must be 2 or 3 (or 4), because dividing 4 and dividing 2 is the same (two 2 and one 4 are the same product), so we consider how to allocate 3 and 2.

Consider a number with M 3 and N 2 added, i.e. \ (x = 3m + 2n\), then the product equals M 3 and N 2 multiplied, define a function,\ (f = 3^m * 2^n = 3^m * 2^{\frac{x-3m}{2}}\), you can Getting $ln F $ is a function that increments m, as shown in the following formula. So the larger the greater the better, M represents 3 of the number, that is, when the decomposition of the time as much as possible to divide a few 3, divided into 2 can not be divided.
\[\begin{split}ln\ f &= m * ln3 + \frac{x-3m}{2} * LN2 \ &= \frac{x}{2} ln \ 2 + (ln\ 3-\frac{3}{2}ln\ 2) *m \e Nd{split}\]

Time complexity O (n), Spatial complexity O (1)

Code
class Solution(object):    def integerBreak(self, n):        """        :type n: int        :rtype: int        """        if(n == 2 or n == 3):            return n-1        res = 1        while(n>=5):            n -= 3            res *= 3        res *= n        return res     

For the following reasons, you can also write this.
If n%3 = = 0, can be decomposed into N/3 3, their product is \ (3^{n/3}\).
if n%3 = = 1, the description and form of N/3 * 3 + 1, one of 3 and a separate 1 is rewritten to 2 2, their product is \ (3^{n/3-1} * 2 * 2\).
if n%3 = = 2, the description and form of N/3 * 3 + 2, their product is \ (3^{N/3} * 2\).

class Solution(object):    def integerBreak(self, n):        """        :type n: int        :rtype: int        """        if n>=4:            if n%3 == 0:                return 3**(n/3)            if n%3 == 1:                return 3**(n/3-1)*4            else:                return 3**(n/3)*2        return n-1

343. Integer Break

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.