We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Furthest Building You Can Reach

Number: 1762

Difficulty: Medium

Paid? No

Companies: Amazon, Google, oyo, TikTok, Dream11, Microsoft, Uber, Apple, DE Shaw, Media.net


Problem Description

Given an array of building heights, along with a fixed number of bricks and ladders, determine the furthest building index you can reach by optimally deciding whether to use bricks or ladders when moving from a lower building to a taller one. When moving between adjacent buildings, you must cover the difference in heights with either ladders (which can cover any height) or bricks (which cover exactly the height difference), unless the next building is of equal or lesser height.


Key Insights

  • Use a greedy approach to prioritize ladders for the largest jumps.
  • Use bricks for smaller height differences.
  • Maintain a min-heap (priority queue) to track the height differences where you chose to use bricks initially.
  • When the number of climbs exceeds the available ladders, the smallest jump (from the heap) is replaced by using bricks.
  • The simulation stops when the bricks run out before you can cover the required jump.

Space and Time Complexity

Time Complexity: O(n log n) in the worst-case scenario due to the heap operations, where n is the number of buildings. Space Complexity: O(n) for the worst-case heap storage.


Solution

We solve the problem by simulating the journey from the first building to the last. For each jump, if the next building is taller than the current one, we calculate the height difference. We push this difference into a min-heap. If the number of required climbs (i.e., the heap's size) exceeds the available ladders, we remove the smallest jump from the heap and use bricks to cover it instead. If at any point the bricks become insufficient (i.e., bricks drop below zero), we cannot progress further and return the current building index. This approach optimally allocates ladders to the largest jumps while using bricks for the smaller ones.


Code Solutions

import heapq

def furthestBuilding(heights, bricks, ladders):
    # Min-heap to store the positive height differences where resources are used
    min_heap = []
    
    # Loop through each building
    for i in range(len(heights) - 1):
        climb = heights[i+1] - heights[i]
        # Only consider climbs that require additional resources
        if climb > 0:
            heapq.heappush(min_heap, climb)
            
        # If we have more climbs than ladders, use bricks for the smallest climb
        if len(min_heap) > ladders:
            bricks -= heapq.heappop(min_heap)
        
        # If bricks run out, return the current building index
        if bricks < 0:
            return i
    
    # If we overcame all challenges, return the last building index
    return len(heights) - 1

# Example usage:
#print(furthestBuilding([4,2,7,6,9,14,12], 5, 1))
← Back to All Questions