Fewest Steps on a Faulty Calculator: Decrement or Double
Greedy & IntervalsAuto-graded · 24 test casesLast updated
Find the minimum number of decrement-by-one and double operations needed to turn one positive integer into another, which rewards working backward from the target with a greedy rule.
A calculator shows the positive integer start on its display. It has only two buttons:
- Decrement: replace the displayed value
xwithx - 1. - Double: replace the displayed value
xwith2 * x.
Each button press counts as one operation, and you may press the buttons in any order and any number of times.
Given the starting value start and the desired value target, return the minimum number of button presses needed to make the display show exactly target.
If start is already equal to target, the answer is 0.
Constraints
startandtargetare positive integers (at least 1).- Values can be large (up to about 10^9), so a search over every reachable value is too slow; aim for a solution that runs in logarithmic time in
target. - Return a single integer.
Examples
Example 1
Input: fewest_button_presses(1, 2)
Output: 1
Example 2
Input: fewest_button_presses(9, 1)
Output: 8
Example 3
Input: fewest_button_presses(2, 10)
Output: 5
Example 4
Input: fewest_button_presses(100, 500)
Output: 41
Hints
Hint 1
Going forward from `start`, it is hard to tell whether to double now or to decrement first. Think about running the process in reverse, starting from `target`.
Hint 2
Reversed, a decrement becomes 'add 1' and a doubling becomes 'divide by 2' (only allowed when the value is even). If the current value is odd, which reverse move is forced? If it is even and still above `start`, which move is better?
Hint 3
Once the reversed value is no larger than `start`, halving no longer helps, and the remaining gap is closed by decrements alone, costing exactly `start - value`.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Work backward from target toward start. Each forward operation has an inverse:
- Decrement
x -> x - 1is reversed byy -> y + 1. - Doubling
x -> 2xis reversed byy -> y / 2, allowed only whenyis even.
While the working value current is larger than start:
- If
currentis odd, halving is impossible, so we must add 1. - If
currentis even, halve it. - Count one operation either way.
When current is no longer above start, the only thing left is to go from start down to current with decrements, costing start - current more operations. If start >= target from the beginning, the answer is simply start - target.
Why it is correct
- Odd value above
start: the only available reverse move is+1, so it is forced. - Even value above
start: suppose an optimal reverse plan adds 1 first. The result is odd, so it must add 1 again before it can halve, reachingcurrent + 2kfor somek >= 1before the first halving. That costs2k + 1operations to reachcurrent/2 + k. Halving first and then addingkreaches the same value in onlyk + 1operations, which is strictly cheaper. So halving immediately is never worse. - Value at or below
start: doubling can never help when the target is below the current value, because each doubling would need to be paid back by extra decrements. Pure decrements cost exactly the difference.
A forward greedy such as "double while below the target" fails; for example, from 100 to 500 it uses 303 operations, whereas the backward method finds 41.
Complexity
- Time:
O(log target). Each halving cuts the value in half, and each+1is immediately followed by a halving. - Space:
O(1).
Reference solution
def fewest_button_presses(start: int, target: int) -> int:
if start >= target:
return start - target
presses = 0
current = target
while current > start:
if current % 2 == 0:
current //= 2
else:
current += 1
presses += 1
return presses + (start - current)