FastPrepFurthest Building with Sandbags and Ropes

Furthest Building with Sandbags and Ropes

Zip logoZip● MediumFULLTIMENEW GRADPHONE SCREEN
Learn

Problem statement

You travel through a row of buildings in order, starting at index 0. Moving to a building of equal or lower height is free. For an upward move of d height units, spend either d sandbags or one rope. A rope covers any upward height difference.

Given heights, the number of available sandbags, and the number of available ropes, return the largest building index you can reach. You may choose which climbs use ropes as you travel.

Function

furthestBuilding(heights: int[], sandbags: int, ropes: int) → int

Examples

Example 1

heights = [4,2,7,6,9,14,12]sandbags = 5ropes = 1return = 4

Use five sandbags for the climb from 2 to 7 and a rope for the climb from 6 to 9. The next uphill move cannot be paid for.

Example 2

heights = [2,6,7]sandbags = 1ropes = 1return = 2

Use the rope for the climb of four and one sandbag for the final climb.

Example 3

heights = [1,5,2,6]sandbags = 4ropes = 0return = 2

After spending four sandbags on the first climb, no resource remains for the final climb.

Constraints

  • 1 <= heights.length <= 100000.
  • 1 <= heights[i] <= 1000000.
  • 0 <= sandbags <= 1000000000.
  • 0 <= ropes <= heights.length.

More Zip problems

See Zip hiring insights
public int furthestBuilding(int[] heights, int sandbags, int ropes) {
    // Write your code here.
}
heights[4,2,7,6,9,14,12]
sandbags5
ropes1
expected4
Checking account…