Fewest Presses Down to Zero
A counter shows n and has three buttons:
- A takes 1 off.
- B halves the number. It works only when the number is even.
- C divides the number by 3. It works only on a multiple of 3.
Return the fewest presses that bring the counter to 0.
Example 1
n 7
returns 4
7, 6, 2, 1, 0 with A, C, B, A: four presses.
Example 2
n 27
returns 4
27, 9, 3, 1, 0: three presses of C and one of A.
Example 3
n 1
returns 1
One press of A.
Constraints
1 <= n <= 10**15
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.