991. Broken Calculator
Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 55% Topics: Math, Greedy
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(logn)
# Space: O(1)
class Solution(object):
def brokenCalc(self, X, Y):
"""
:type X: int
:type Y: int
:rtype: int
"""
result = 0
while X < Y:
if Y%2:
Y += 1
else:
Y /= 2
result += 1
return result + X-Y
Solution from kamyu104/LeetCode-Solutions · MIT
Similar questions