991. Broken Calculator - notruilin/LeetCode GitHub Wiki
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.
Consider Y -> X, when Y > X
if Y % 2 == 0, we always divide Y by 2, because
e.g. 12 -> 7, due to 12 % 2 == 0
12 / 2 + 1 = 7, two steps
12 + 1 cannot divide by 2
(12 + 1 + 1) / 2 = 7, three steps
(a + 1 + 1) / 2 VS a / 2 +1
11 -> 7, due to 11 % 2 != 0
(11 + 1) / 2 + 1 = 7, three steps
(11 + 1 + 1 + 1) / 2 = 7, four steps
(a + 1 + 1 + 1) / 2 VS (a + 1) / 2 +1
Until Y < X, then keep +1 +1 ...