Python simple solution


  • 0
    class Solution(object):
        def count(self, n, d):
            if n in d: return d[n]
            if n == 1: d[n] = 0
            elif n%2 == 0: d[n] = 1 + self.count(n/2, d)
            else: d[n] = 1 + min(self.count(n-1, d), self.count(n+1, d))
            return d[n]
        
        def integerReplacement(self, n):
            return self.count(n, {})
    

    I know this is not the best solution but is the simplest, I guess.


Log in to reply
 

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