Untitled

 avatar
unknown
plain_text
11 days ago
754 B
0
Indexable
import java.util.*;

class Solution {
    public int furthestBuilding(int[] heights, int bricks, int ladders) {
        // Using PriorityQueue with custom comparator for max heap
        PriorityQueue<Integer> pq = new PriorityQueue<>();
        
        for (int i = 0; i < heights.length - 1; i++) {
            int jumpLen = heights[i + 1] - heights[i];
            
            if (jumpLen > 0) {
                pq.offer(jumpLen);
                
                if (pq.size() > ladders) {
                    bricks -= pq.poll();
                }
                
                if (bricks < 0) {
                    return i;
                }
            }
        }
        
        return heights.length - 1;
    }
}
Leave a Comment