blob: 8d7dbdab683fa1b473b9d1429f9341b33272340a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# 397. Integer Replacement
"""
given a positive integer 'n', you can apply one of the following operations:
if 'n' is even, replace 'n' witn 'n / 2'. if 'n' is odd, replace 'n' with
either 'n + 1' or 'n - 1'. return the minimum number of operations needed for
'n' to become 1
"""
class Solution(object):
def integerReplacement(self, n):
"""
:type n: int
:rtype: int
"""
ans = 0
while n > 1:
ans += 1
if n % 2 == 0:
n //= 2
elif n % 4 == 1 or n == 3:
n -= 1
else:
n += 1
return ans
if __name__ == "__main__":
obj = Solution()
print(obj.integerReplacement(n=8))
print(obj.integerReplacement(n=7))
print(obj.integerReplacement(n=4))
|