FunctionRacerProblemsRace

Three Button Calculator

BeginnerDynamic programmingPython, JavaScript, TypeScript, Java, C++

A calculator shows the number 1. It has three buttons: one adds 1 to the shown number, one doubles it and one triples it.

Return the fewest button presses that make the calculator show exactly target. When target is 1, no press is needed, so return 0.

Example 1

target 10
returns 3

Triple to 3, triple to 9, then add 1. Halving back from 10 first would take four presses.

Example 2

target 7
returns 3

Add 1 to get 2, triple to 6, then add 1.

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 Dynamic programming problems