Created
February 1, 2019 13:48
-
-
Save jan25/9c4ea081055cdbba8ef16090cf427055 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def integerReplacement(self, n): | |
steps = 0 | |
while n > 1: | |
if n == 3: | |
steps += 2; break | |
if n & 1 ^ 1: | |
n >>= 1 | |
else: | |
if (n & 3) == 3: | |
n += 1 | |
else: n -= 1 | |
steps += 1 | |
return steps |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment