FunctionRacerProblemsRace

Fewest Presses Down to Zero

HardRecursionPython, JavaScript, TypeScript, Java, C++

A counter shows n and has three buttons:

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

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Recursion problems