-
Notifications
You must be signed in to change notification settings - Fork 614
/
991.py
33 lines (26 loc) · 817 Bytes
/
991.py
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
'''
On a broken calculator that has a number showing on its display, we can perform two operations:
Double: Multiply the number on the display by 2, or;
Decrement: Subtract 1 from the number on the display.
Initially, the calculator is displaying the number X.
Return the minimum number of operations needed to display the number Y.
Example 1:
Input: X = 2, Y = 3
Output: 2
Explanation: Use double operation and then decrement operation {2 -> 4 -> 3}.
'''
class Solution(object):
def brokenCalc(self, X, Y):
"""
:type X: int
:type Y: int
:rtype: int
"""
if X == Y:
return 0
if X > Y:
return X-Y
if(Y%2 == 1):
return 1 + self.brokenCalc(X, Y+1)
else:
return 1 + self.brokenCalc(X, Y/2)