343. Integer break

Source: Internet
Author: User

Given a positive integerN, Break it into the sumAt leastTwo positive integers and maximize the product of those integers. Return the maximum product you can get.

For example, givenN= 2, return 1 (2 = 1 + 1); givenN= 10, return 36 (10 = 3 + 3 + 4 ).

Note: You may assume thatNIs not less than 2.

Hint:

  1. There is a simple O (n) solution to this problem.
  2. You may check the breaking resultsNRanging from 7 to 10 to discover the regularities.
Question:

Given to a positive integer N, it is divided into at least two positive integers and the sum of the integers maximizes the product of these integers. Returns the maximum product that can be obtained.

The test is described as a question.

Note:Assume N is not less than 2.

Tip:

  1. The problem is solved by O (n.
  2. You can find the law by calculating the integer n between 7 and 10.
Solution:

Observe the Search rules for situations where N is 7 to 10:

7  -> 3 * 4     -> 128  -> 2 * 3 * 3 -> 189  -> 3 * 3 * 3 -> 2710 -> 3 * 3 * 4 -> 36

It can be found that the difference value of each element in the split result cannot exceed 1.

Solution I integer spread

Spread integer n to an integer whose difference does not exceed 1. m is enumerated from 2 to n.

Python code:
class Solution(object):    def integerBreak(self, n):        """        :type n: int        :rtype: int        """        return max([reduce(operator.mul, self.splitInt(n, m)) for m in range(2, n + 1)])        def splitInt(self, n, m):        quotient = n / m        remainder = n % m        return [quotient] * (m - remainder) + [quotient + 1] * remainder

 

The complexity of the above Code is actually O (N ^ 2), which can be simplified to O (n ).

Python code:
class Solution(object):    def integerBreak(self, n):        """        :type n: int        :rtype: int        """        return max([self.mulSplitInt(n, m) for m in range(2, n + 1)])        def mulSplitInt(self, n, m):        quotient = n / m        remainder = n % m        return quotient ** (m - remainder) * (quotient + 1) ** remainder

The above solution can further optimize the time complexity to O (1). Thanks to lost_in.

Observe N from 2 to 13:

2  ->  1 * 13  ->  2 * 14  ->  2 * 25  ->  3 * 26  ->  3 * 37  ->  3 * 2 * 28  ->  3 * 3 * 29  ->  3 * 3 * 310 ->  3 * 3 * 2 * 211 ->  3 * 3 * 3 * 212 ->  3 * 3 * 3 * 313 ->  3 * 3 * 3 * 2 * 2

The following rule can be found:

When N/3 <= 1, it is divided into the product of two numbers. If we try to evenly share N/3> 1, it is divided into the product N % 3 = 0 of several 3 and 2, when the product N % 3 = 1 is divided into n-1 3 and the product N % 3 = 2 is divided into N-3 and the product N % 3 = 2.
Python code:
class Solution(object):    def integerBreak(self, n):        """        :type n: int        :rtype: int        """        div = n / 3        if div <= 1:            return (n / 2) * (n / 2 + n % 2)        mod = n % 3        if mod == 0:            return 3 ** div        elif mod == 1:            return 3 ** (div - 1) * 4        elif mod == 2:            return 3 ** div * 2

 

Solution II Dynamic Planning

DP [I] indicates the maximum product that can be obtained by integer I splitting. DP [I] is only related to the DP [I-2] and DP [I-3] States.

Obtain the state transition equation:

dp[x] = max(3 * dp[x - 3], 2 * dp[x - 2])

When X is <= 3, the result must be judged.

Python code:
class Solution(object):    def integerBreak(self, n):        """        :type n: int        :rtype: int        """        if n <= 3: return n - 1        dp = [0] * (n + 1)        dp[2], dp[3] = 2, 3        for x in range(4, n + 1):            dp[x] = max(3 * dp[x - 3], 2 * dp[x - 2])        return dp[n]

 

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.