O(1) Solution Python


  • 0
    S

    Found a pattern.

    9: 3 x 3 x 3     % 3 == 0 ,,, 3^(n/3)
    10 3 x 3 x 2 x 2 % 3 == 1 ,,, 3^(n/3 - 1) * 2 * 2
    11 3 x 3 x 3 x 2 % 3 == 2 ,,, 3^(n/3) * 2
    

    The related code that implements above pattern

    import math
    
    class Solution(object):
        def integerBreak(self, n):
            if n == 2: return 1
            if n == 3: return 2
            if n == 4: return 4
            if n % 3 == 0:
                return 3**(n / 3)
            if n % 3 == 1:
                return 3**(n / 3 - 1) * 4
            if n % 3 == 2:
                return 3**(n / 3) * 2
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.