-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathSolution.java
30 lines (25 loc) · 986 Bytes
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import java.util.Collections;
import java.util.PriorityQueue;
class Solution {
public int furthestBuilding(int[] heights, int bricks, int ladders) {
PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
for (int i = 0; i < heights.length - 1; i++) {
if (heights[i] >= heights[i + 1])
continue;
bricks -= heights[i + 1] - heights[i];
pq.add(heights[i + 1] - heights[i]);
if (bricks < 0) {
bricks += pq.poll();
if (ladders > 0)
ladders--;
else
return i;
}
}
return heights.length - 1;
}
public static void main(String[] args) {
System.out.println(new Solution().furthestBuilding(new int[] { 4, 2, 7, 6, 9, 14, 12 }, 5, 1));
System.out.println(new Solution().furthestBuilding(new int[] { 4, 12, 2, 7, 3, 18, 20, 3, 19 }, 10, 2));
}
}