Untitled
unknown
plain_text
a year ago
754 B
4
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;
}
}Editor is loading...
Leave a Comment