Skip to content

Instantly share code, notes, and snippets.

@jan25
Created February 1, 2019 13:48
Show Gist options
  • Save jan25/9c4ea081055cdbba8ef16090cf427055 to your computer and use it in GitHub Desktop.
Save jan25/9c4ea081055cdbba8ef16090cf427055 to your computer and use it in GitHub Desktop.
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