Untitled
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<>(Collections.reverseOrder()); 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