As reported: Flexport SDE-1 (2025), coding round 1, second question: given an array, count how far you can move starting at index 0. Moving to the next element is free if it is lower; if it is higher you must pay with either a supercharge or the battery, and the battery can pay a climb only if the difference is at most the remaining battery. Example:
[50, 150, 100, 250, 200, 400, 500, 300, 600], battery200,2superchargers →7.
This is LeetCode 1642 — Furthest Building You Can Reach, with bricks as the battery and ladders as superchargers. Our write-up: /furthest-building-you-can-reach.
You start at index 0 of heights with a battery of bricks units and ladders superchargers. Moving from i to i + 1:
- if
heights[i + 1] ≤ heights[i], it is free; - otherwise the climb
d = heights[i + 1] − heights[i]is paid either withdbattery units (if at leastdremain) or with one supercharger.
Return the furthest index you can reach, using the battery and superchargers optimally.
Example 1
heights = [50, 150, 100, 250, 200, 400, 500, 300, 600], bricks (battery) = 200, ladders (superchargers) = 2
0→1: battery 100; 1→2 free; 2→3: supercharger; 3→4 free; 4→5: supercharger;
5→6: battery 100; 6→7 free; 7→8 needs 300 — nothing left.
Output: 7
Example 2
heights = [4, 2, 7, 6, 9, 14, 12], bricks = 5, ladders = 1
Output: 4
Example 3
heights = [4, 12, 2, 7, 3, 18, 20, 3, 19], bricks = 10, ladders = 2
Output: 7
Constraints: 1 ≤ heights.length ≤ 10^5, 1 ≤ heights[i] ≤ 10^6, 0 ≤ bricks ≤ 10^9, 0 ≤ ladders ≤ heights.length.
Code runner format: three JSON values, one per line: heights, bricks, ladders. Implement furthestBuilding(heights, bricks, ladders).