You are given an integer array heights
representing the heights of buildings, some bricks
, and some ladders
. You start your journey from building 0
and move to the next building by possibly using bricks or ladders.
While moving from building i
to building i+1
(0-indexed):
(h[i+1] - h[i])
bricks.Return the furthest building index (0-indexed) you can reach if you use the given ladders and bricks optimally.
Example 1:
heights = [4,2,7,6,9,14,12], bricks = 5, ladders = 1
Output: 4
Explanation: Starting at building 0, you can follow these steps:
It is impossible to go beyond building 4 because you do not have any more bricks or ladders.
Example 2:
heights = [4,12,2,7,3,18,20,3,19], bricks = 10, ladders = 2
Output: 7
Could you provide an algorithm to solve this problem efficiently?
A brute-force approach would involve trying every possible combination of using bricks and ladders to reach the furthest building. For each possible path, we check if we have enough bricks and ladders to traverse it. We keep track of the maximum building index reachable across all valid paths.
def furthest_building_naive(heights, bricks, ladders):
def solve(index, current_bricks, current_ladders):
if index >= len(heights) - 1:
return index
if heights[index + 1] <= heights[index]:
return solve(index + 1, current_bricks, current_ladders)
diff = heights[index + 1] - heights[index]
use_bricks = -1
use_ladder = -1
if current_bricks >= diff:
use_bricks = solve(index + 1, current_bricks - diff, current_ladders)
if current_ladders > 0:
use_ladder = solve(index + 1, current_bricks, current_ladders - 1)
return max(use_bricks, use_ladder)
return solve(0, bricks, ladders)
We can use a greedy approach combined with a min-heap to solve this problem efficiently. The idea is to always use bricks for the smaller height differences and use ladders for the larger height differences. We maintain a min-heap to store the height differences for which we have used bricks so far. If we run out of bricks, we can replace the smallest height difference in the heap with a ladder.
heights
array.heights[i+1] <= heights[i]
, continue to the next building.diff = heights[i+1] - heights[i]
.diff
to a min-heap.i
.heights
array, we can reach the last building, so return len(heights) - 1
.import heapq
def furthest_building(heights, bricks, ladders):
heap = [] # Min-heap to store height differences for bricks
for i in range(len(heights) - 1):
diff = heights[i + 1] - heights[i]
if diff <= 0:
continue
heapq.heappush(heap, diff)
if sum(heap) > bricks:
if ladders > 0:
smallest_diff = heapq.heappop(heap)
ladders -= 1
else:
return i
return len(heights) - 1
heights
is empty, return 0. (Although the problem statement says the length is always >= 1)bricks
and ladders
are 0, the function should return 0 if the height of the next building is greater than the current building.len(heights) - 1
.len(heights) - 1
.heights = [4, 2, 7, 6, 9, 14, 12], bricks = 5, ladders = 1
i = 0
, heights[1] <= heights[0]
, continuei = 1
, diff = 7 - 2 = 5
, heap = [5]
i = 2
, heights[3] <= heights[2]
, continuei = 3
, diff = 9 - 6 = 3
, heap = [3, 5]
i = 4
, diff = 14 - 9 = 5
, heap = [3, 5, 5]
sum(heap) = 13 > bricks = 5
, ladders > 0
, smallest_diff = 3
, heap = [5, 5]
, ladders = 0
i = 5
, diff = 12 - 14 = -2
, continuelen(heights) - 1 = 6
(Note: There's an error in the initial calculation that makes the example incorrect, it should return 4)