Furthest Building with Bricks and Ladders
Updated
Problem statement
You are given an integer array heights, where heights[i] is the height of building i. You start at building 0 and move to the next building one step at a time, from building i to building i + 1.
When you move from building i to building i + 1:
- If
heights[i] >= heights[i + 1], the move is free. - If
heights[i] < heights[i + 1], you must use either one ladder or exactlyheights[i + 1] - heights[i]bricks.
You have bricks bricks and ladders ladders. Each brick and each ladder can be used only once, and one ladder covers a climb of any height.
Return the largest building index you can reach if you choose the bricks and ladders for each climb in the best way.
Function
furthestBuilding(heights: int[], bricks: int, ladders: int) → intExamples
Example 1
heights = [3,1,4,8,6,10]bricks = 4ladders = 1return = 4The climbs are 1 → 4 (3), 4 → 8 (4), and 6 → 10 (4). The moves 3 → 1 and 8 → 6 are free.
To reach building 4, use the ladder on the climb of 4 and three bricks on the climb of 3. One brick is left.
Building 5 would need all three climbs paid for. One ladder covers one of them, and the other two need at least 3 + 4 = 7 bricks, but there are only 4. So the answer is 4.
Example 2
heights = [1,2,3,100]bricks = 2ladders = 1return = 3The climbs are 1, 1, and 97. Use the two bricks on the two small climbs and save the ladder for the climb of 97. You reach the last building, index 3.
If you used the ladder on the first climb, you could not pay for the climb of 97 with one brick.
Example 3
heights = [5,5,5]bricks = 0ladders = 0return = 2Every move is to a building of the same height, so every move is free and you reach index 2.
Example 4
heights = [2,7,4,9]bricks = 0ladders = 1return = 2Use the ladder for the climb from 2 to 7. The move from 7 to 4 is free. The climb from 4 to 9 needs 5 bricks or a ladder, and you have neither, so you stop at index 2.
Constraints
1 <= heights.length <= 10^51 <= heights[i] <= 10^60 <= bricks <= 10^90 <= ladders <= heights.length
More Amazon Phone Screen questions
- Number of IslandsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 3SumPHONE SCREEN · Seen Nov 2026
- Circular Primes Below a LimitPHONE SCREEN · Seen Nov 2026
- Cheapest Flights Within K StopsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Course Schedule IIPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Two SumOA · PHONE SCREEN · Seen Nov 2026
- Count the 1 Bits in a 32-Bit ValuePHONE SCREEN · Seen Nov 2026
Similar questions at other companies
- Tennis Court SchedulingAtlassian · PHONE SCREEN · ONSITE INTERVIEWAtlassian interview questions
- Maximum Number of Events That Can Be AttendedByteDance · PHONE SCREENByteDance interview questions
- Defer Expenses to Preserve BalanceDRW · PHONE SCREENDRW interview questions
- Minimize Array Sum After K Halving OperationsDocuSign · PHONE SCREENDocuSign interview questions
- Maximum Number of Events That Can Be AttendedSnowflake · PHONE SCREENSnowflake interview questions
- Automation Pipeline, Part 3: Stage DurationPlaid · PHONE SCREENPlaid interview questions
- Finish Ordered CPU Tasks with the Fewest CPUsGoogle · PHONE SCREENGoogle interview questions
- Minimum Sum After K Ceiling HalvingsAdobe · PHONE SCREENAdobe interview questions
- Maximize the Minimum Cone DistanceSumo Logic · PHONE SCREEN
- Schedule Unit-Time Jobs for the Most ProfitScale AI · PHONE SCREENScale AI interview questions
Amazon interview experiences
- Amazon SWE OA with AI Coding: 1 Experience
- Amazon SWE Intern Phone Screen Interview Experience: 1 Experience
- Amazon New Grad SDE I OA and Virtual Onsite: 1 Experience
- Amazon BIE Intern SQL and Interview Questions: 17 Experiences
- Amazon Experienced SWE AI-Assisted Debugging OA: 1 Experience
- Amazon SDE II (L5) Interview Questions: 5 Experiences