Skip to content
Decoder

Furthest Building You Can Reach

Medium

Problem

You walk along a row of buildings with heights heights, starting at building 0. Moving to the next building is free if it is the same height or lower. If it is taller, you must climb the difference using either one ladder (any height) or that many bricks.

You have bricks bricks and ladders ladders. Using them as cleverly as possible, return the furthest building index you can reach.

Example 1

Input
heights = [4, 2, 7, 6, 9, 14, 12], bricks = 5, ladders = 1
Output
4

Climb 2 → 7 with 5 bricks, then 6 → 9 with the ladder. 9 → 14 needs 5 more bricks and you have none, so you stop at index 4.

Constraints

  • 1 <= heights.length <= 10^5
  • 1 <= heights[i] <= 10^6
  • 0 <= bricks <= 10^9
  • 0 <= ladders <= heights.length

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.