[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/Monika-R/LeetCode-Solutions/master/Python/integer-break.py [Back]  [Original]

# Time:  O(logn), pow is O(logn).
# Space: O(1)

class Solution(object):
    def integerBreak(self, n):
        """
        :type n: int
        :rtype: int
        """
        if n < 4:
            return n - 1

        #  Proof.
        #  1. Let n = a1 + a2 + ... + ak, product = a1 * a2 * ... * ak
        #      - For each ai >= 4, we can always maximize the product by:
        #        ai = 5, we can always maximize the product by:
        #        aj = 4, the max of the product must be in the form of
        #        3^a * 2^b, s.t. 3a + 2b = n
        #
        #  2. To maximize the product = 3^a * 2^b s.t. 3a + 2b = n
        #      - For each b >= 3, we can always maximize the product by:
        #        3^a * 2^b = 4, the max of the product must be in the form of
        #        3^Q * 2^R, 0 

Web Proxy Viewer  |  New URL  |  Original Page