Skip to the code
cf.
+10 XP

#491 Cheapest Hop to the Top Ledge

easy

In a browser platform game, a character climbs a column of ledges numbered from 0. Jumping off ledge i costs cost[i] energy, and each jump goes up one or two ledges. The character may start on ledge 0 or ledge 1 for free.

The goal is the top, one place above the last ledge. Write cheapestClimb(cost) that returns the least total energy needed to reach the top.

Example 1

Input: cost = [12,18,25]
Output: 18

Start on ledge 1, pay 18, and jump two ledges to the top.

Example 2

Input: cost = [2,50,2,2,2,50,2,50,2]
Output: 10

Jump off five ledges that cost 2 each and never touch a ledge that costs 50.

Constraints

  • 2 <= cost.length <= 1000
  • 0 <= cost[i] <= 999
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).