Skip to the code
cf.
+10 XP

#430 Fewest Zoom Presses

easy

A map widget has three zoom buttons: "+1" adds one to the zoom level, "-1" subtracts one, and "x2" doubles it. The zoom level can never go below 0 or above 2000, so a press that would leave that range does nothing useful and isn't allowed.

Write fewestPresses(start, target) that returns the fewest button presses needed to change the zoom level from start to target.

Example 1

Input: start = 3, target = 10
Output: 3

For example 3 → 4 → 5 → 10 (+1, +1, x2). Two presses from 3 can only reach 1, 3, 4, 5, 7, 8, or 12.

Example 2

Input: start = 5, target = 5
Output: 0

The zoom level is already right.

Constraints

  • 0 <= start, target <= 1000
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).