Description
Given a calculator that can double or decrement by 1, return minimum operations to transform startValue to target.
Examples
Input:
startValue = 2, target = 3Output:
2Explanation:
2×2=4, 4-1=3.
Input:
startValue = 1, target = 1024Output:
10Explanation:
Since target > startValue, it is possible to only multiply by 2. The requirement is 1×2¹⁰ = 1024, so multiplying by 2 exactly 10 times: 1→2→4→8→16→32→64→128→256→512→1024.
Input:
startValue = 10, target = 1Output:
9Explanation:
Since target < startValue, it is possible to only subtract 1. The task requires go from 10 to 1, which requires 9 subtraction operations: 10→9→8→7→6→5→4→3→2→1.
Constraints
- •
1 ≤ startValue, target ≤ 10⁹