Program to find out the farthest building a parkour artist can reach in Python

A parkour artist wants to traverse buildings of different heights using a limited number of bricks and ladders. Bricks are used for small height differences (each brick covers 1 unit), while ladders can cover any height difference. We need to find the farthest building the artist can reach.

Problem Understanding

Given an array of building heights, the artist starts at building 0 and moves sequentially. When moving to a taller building, they must use bricks or ladders. The goal is to reach the farthest possible building using the available resources optimally.

Example Walkthrough

For heights = [5, 8, 7, 6, 2, 3, 1, 4] with 3 bricks and 2 ladders:

  • Building 0 ? 1: Height difference = 3, use 3 bricks

  • Building 1 ? 2, 2 ? 3, 3 ? 4: No resources needed (going down)

  • Building 4 ? 5: Height difference = 1, use 1 ladder

  • Building 5 ? 6: No resources needed (going down)

  • Building 6 ? 7: Height difference = 3, use 1 ladder

The artist reaches building 7 (index 7), so the answer is 7.

Algorithm Strategy

We use a greedy approach with a min-heap. The key insight is to use ladders for the largest height differences and bricks for smaller ones. We track height differences in a heap and swap bricks for ladders when needed.

Implementation

from heapq import heappush, heappop

def solve(heights, bricks, ladders):
    temp = []  # min-heap to store negative height differences
    
    for i in range(1, len(heights)):
        dist = heights[i] - heights[i - 1]
        
        if dist > 0:  # Moving to a taller building
            bricks -= dist  # Try using bricks first
            heappush(temp, -dist)  # Store negative for min-heap behavior
            
            if bricks < 0:  # Not enough bricks
                ladders -= 1  # Use a ladder instead
                bricks -= heappop(temp)  # Get back the largest difference used
                
                if bricks < 0 or ladders < 0:  # Out of resources
                    return i - 1
    
    return len(heights) - 1  # Reached the end

# Test the function
heights = [5, 8, 7, 6, 2, 3, 1, 4]
bricks = 3
ladders = 2

result = solve(heights, bricks, ladders)
print(f"Farthest building reachable: {result}")
Farthest building reachable: 7

How It Works

The algorithm maintains a heap of height differences where bricks were used. When bricks run out, it uses a ladder to cover the largest height difference (retrieved from the heap) and gets those bricks back for smaller differences.

Step-by-Step Execution

def solve_with_trace(heights, bricks, ladders):
    temp = []
    print(f"Starting with {bricks} bricks and {ladders} ladders")
    
    for i in range(1, len(heights)):
        dist = heights[i] - heights[i - 1]
        print(f"\nBuilding {i-1} ? {i}: height {heights[i-1]} ? {heights[i]}")
        
        if dist > 0:
            print(f"  Height difference: {dist}")
            bricks -= dist
            heappush(temp, -dist)
            print(f"  Used {dist} bricks, remaining: {bricks}")
            
            if bricks < 0:
                largest_diff = -heappop(temp)
                ladders -= 1
                bricks += largest_diff
                print(f"  Used ladder for largest difference ({largest_diff})")
                print(f"  Remaining: {bricks} bricks, {ladders} ladders")
                
                if bricks < 0 or ladders < 0:
                    print(f"  Out of resources! Can reach building {i-1}")
                    return i - 1
        else:
            print(f"  Going down, no resources needed")
    
    print(f"\nReached the end! Farthest building: {len(heights)-1}")
    return len(heights) - 1

# Trace through the example
result = solve_with_trace([5, 8, 7, 6, 2, 3, 1, 4], 3, 2)
Starting with 3 bricks and 2 ladders

Building 0 ? 1: height 5 ? 8
  Height difference: 3
  Used 3 bricks, remaining: 0

Building 1 ? 2: height 8 ? 7
  Going down, no resources needed

Building 2 ? 3: height 7 ? 6
  Going down, no resources needed

Building 3 ? 4: height 6 ? 2
  Going down, no resources needed

Building 4 ? 5: height 2 ? 3
  Height difference: 1
  Used 1 bricks, remaining: -1
  Used ladder for largest difference (3)
  Remaining: 2 bricks, 1 ladders

Building 5 ? 6: height 3 ? 1
  Going down, no resources needed

Building 6 ? 7: height 1 ? 4
  Height difference: 3
  Used 3 bricks, remaining: -1
  Used ladder for largest difference (3)
  Remaining: 2 bricks, 0 ladders

Reached the end! Farthest building: 7

Time and Space Complexity

  • Time Complexity: O(n log n) due to heap operations

  • Space Complexity: O(n) for storing height differences in the heap

Conclusion

This greedy algorithm efficiently solves the parkour problem by using ladders for the largest height differences and bricks for smaller ones. The heap data structure enables optimal resource allocation by allowing us to swap bricks for ladders when needed.

Updated on: 2026-03-26T13:57:02+05:30

246 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements