This approach uses a greedy strategy combined with a max-heap (priority queue) to determine the optimal use of bricks and ladders. The idea is to always use bricks first for the smallest height difference and use ladders for the largest, potentially reserving the ladders for future taller buildings. Whenever we encounter a new jump (difference in height), we add it to the heap, and if the total number of jumps exceeds the available ladders, we use bricks for the smallest jump. If we don’t have enough bricks, we can’t proceed further.
Time Complexity: O(n log n) due to sorting.
Space Complexity: O(n) for storing height differences.
1import heapq
2
3def furthest_building(heights, bricks, ladders):
4 min_heap = []
5 for i in range(len(heights) - 1):
6 diff = heights[i + 1] - heights[i]
7 if diff > 0:
8 heapq.heappush(min_heap, diff)
9 if len(min_heap) > ladders:
10 bricks -= heapq.heappop(min_heap)
11 if bricks < 0:
12 return i
13 return len(heights) - 1
14
15# Sample run
16heights = [4, 2, 7, 6, 9, 14, 12]
17print(furthest_building(heights, 5, 1)) # Output: 4
18
This Python solution efficiently uses a min-heap to optimize the allocation of ladders and bricks. By storing the smallest height differences, it ensures that the smallest climbs are covered by bricks once ladders run out, similar to priority queue usage in other language solutions.
This approach employs binary search to find the furthest building we can reach. The binary search is performed over the indices of the buildings (0 to n-1). For each index midpoint in the search, we check if it's possible to reach that building, given the constraints of available bricks and ladders using a helper function that checks feasibility by simulating resource allocation greedily. The search continually refines the possible maximum index based on resource ability.
Time Complexity: O(n log n) because of binary search with operations similar to heap for each midpoint.
Space Complexity: O(n) to store the min-heap simulation for each search step.
1import heapq
2
3def can_reach(heights, bricks, ladders, target):
4 min_heap = []
5 for i in range(target):
6 diff = heights[i + 1] - heights[i]
7 if diff > 0:
8 heapq.heappush(min_heap, diff)
9 if len(min_heap) > ladders:
10 bricks -= heapq.heappop(min_heap)
11 if bricks < 0:
12 return False
13 return True
14
15def furthest_building(heights, bricks, ladders):
16 low, high = 0, len(heights) - 1
17 result = 0
18 while low <= high:
19 mid = (low + high) // 2
20 if can_reach(heights, bricks, ladders, mid):
21 result = mid
22 low = mid + 1
23 else:
24 high = mid - 1
25 return result
26
27# Example usage
28heights = [4, 12, 2, 7, 3, 18, 20, 3, 19]
29print(furthest_building(heights, 10, 2)) # Output: 7
30
Applying binary search in this Python solution effectively searches for the maximum index of reachable buildings using a helper function, can_reach, which factors in available ladders and bricks via a min-heap metaphor. This strategy balances resource usage constraints at each binary search stage.