1642. Furthest Building You Can Reach
## description
## 1642. Furthest Building You Can Reach
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),
- –If the current building's height is greater than or equal to the next building's height, you do not need a ladder or bricks.
- –If the current building's height is less than the next building's height, you can either use one ladder or (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:
1Input: heights = [4,2,7,6,9,14,12], bricks = 5, ladders = 1
2Output: 4
3Explanation: Starting at building 0, you can follow these steps:
4- Go to building 1 without using ladders nor bricks since 4 >= 2.
5- Go to building 2 using 5 bricks. You must use either bricks or ladders because 2 < 7.
6- Go to building 3 without using ladders nor bricks since 7 >= 6.
7- Go to building 4 using your only ladder. You must use either bricks or ladders because 6 < 9.
8It is impossible to go beyond building 4 because you do not have any more bricks or ladders.Example 2:
1Input: heights = [4,12,2,7,3,18,20,3,19], bricks = 10, ladders = 2
2Output: 7Example 3:
1Input: heights = [14,3,19,3], bricks = 17, ladders = 0
2Output: 3- –1 <= heights.length <= 105
- –1 <= heights[i] <= 106
- –0 <= bricks <= 109
- –0 <= ladders <= heights.length
Constraints:
## notes
### Intuition
Ladders can cover any height difference, so they're most valuable for the largest jumps. By greedily using bricks first and retroactively converting the largest brick usage to a ladder when we run out, we maximize our reach.
### Implementation
Use a max heap to track jump sizes. For each positive height difference, assume we use bricks and add the difference to the heap. If bricks go negative, we need a ladder—if none remain, return the current index. Otherwise, use a ladder by popping the largest jump from the heap and adding those bricks back. If we complete the iteration, return the last index.
### Edge-cases
Python's heapq is a min heap, so negate values to simulate a max heap. Remember to negate again when popping.
- –Time: O(n log n) — heap operations for each building
- –Space: O(n) — heap stores up to n jumps
### Complexity
## solution
1from typing import List
2import heapq
3
4class Solution:
5 def furthestBuilding(self, heights: List[int], bricks: int, ladders: int) -> int:
6 max_heap = []
7 for i in range(len(heights) - 1):
8 diff = heights[i + 1] - heights[i]
9 if diff <= 0:
10 continue
11
12 bricks -= diff
13 heapq.heappush(max_heap, -diff)
14
15 if bricks < 0:
16 if ladders == 0:
17 return i
18 ladders -= 1
19 bricks += -heapq.heappop(max_heap)
20 return len(heights) - 1
21
22
23
24
25if __name__ == "__main__":
26 # Include one-off tests here or debugging logic that can be run by running this file
27 # e.g. print(solution.two_sum([1, 2, 3, 4], 3))
28 solution = Solution()
29