We use a Min-Heap to keep track of the largest height differences where we used a ladder. If the number of height differences exceeds the number of ladders, we replace the smallest difference (using the ladder) with bricks.
- Initialize a min-heap
h. - For each building from
i = 0tolen(heights) - 2:- Calculate
diff = heights[i+1] - heights[i]. - If
diff > 0:- Push
diffto the heap. - If
len(h) > ladders:- Pop the smallest
difffrom the heap. - Subtract that
difffrombricks.
- Pop the smallest
- If
bricks < 0, return current indexi.
- Push
- Calculate
- If we finish the loop, return
len(heights) - 1.
- Time Complexity: O(N log L), where L is the number of ladders.
- Space Complexity: O(L).
import heapq
def furthest_building(heights, bricks, ladders):
heap = []
for i in range(len(heights) - 1):
diff = heights[i+1] - heights[i]
if diff > 0:
heapq.heappush(heap, diff)
if len(heap) > ladders:
bricks -= heapq.heappop(heap)
if bricks < 0:
return i
return len(heights) - 1