This approach uses a greedy strategy combined with a max-heap (priority queue) to determine the optimal use of bricks and ladders. The idea is to always use bricks first for the smallest height difference and use ladders for the largest, potentially reserving the ladders for future taller buildings. Whenever we encounter a new jump (difference in height), we add it to the heap, and if the total number of jumps exceeds the available ladders, we use bricks for the smallest jump. If we don’t have enough bricks, we can’t proceed further.
Time Complexity: O(n log n) due to sorting.
Space Complexity: O(n) for storing height differences.
1import java.util.PriorityQueue;
2
3public class FurthestBuilding {
4 public static int furthestBuilding(int[] heights, int bricks, int ladders) {
5 PriorityQueue<Integer> minHeap = new PriorityQueue<>();
6 for (int i = 0; i < heights.length - 1; ++i) {
7 int climb = heights[i + 1] - heights[i];
8 if (climb > 0) {
9 minHeap.add(climb);
10 if (minHeap.size() > ladders) {
11 bricks -= minHeap.poll();
12 if (bricks < 0) return i;
13 }
14 }
15 }
16 return heights.length - 1;
17 }
18
19 public static void main(String[] args) {
20 int[] heights = {4, 12, 2, 7, 3, 18, 20, 3, 19};
21 System.out.println(furthestBuilding(heights, 10, 2)); // Output: 7
22 }
23}
24
Similar to the C++ solution, this Java code employs a priority queue to manage the differences in heights. It ensures that the smallest differences are compensated using bricks when the number of jumps exceeds the ladder allowance, hence maintaining optimal use of resources.
This approach employs binary search to find the furthest building we can reach. The binary search is performed over the indices of the buildings (0 to n-1). For each index midpoint in the search, we check if it's possible to reach that building, given the constraints of available bricks and ladders using a helper function that checks feasibility by simulating resource allocation greedily. The search continually refines the possible maximum index based on resource ability.
Time Complexity: O(n log n) because of binary search with operations similar to heap for each midpoint.
Space Complexity: O(n) to store the min-heap simulation for each search step.
1import java.util.PriorityQueue;
2
3public class FurthestBuilding {
4 private static boolean canReach(int[] heights, int bricks, int ladders, int target) {
5 PriorityQueue<Integer> minHeap = new PriorityQueue<>();
6 for (int i = 0; i < target; ++i) {
7 int diff = heights[i + 1] - heights[i];
8 if (diff > 0) {
9 minHeap.add(diff);
10 if (minHeap.size() > ladders) {
11 bricks -= minHeap.poll();
12 if (bricks < 0) return false;
13 }
14 }
15 }
16 return true;
17 }
18
19 public static int furthestBuilding(int[] heights, int bricks, int ladders) {
20 int low = 0, high = heights.length - 1, result = 0;
21 while (low <= high) {
22 int mid = low + (high - low) / 2;
23 if (canReach(heights, bricks, ladders, mid)) {
24 result = mid;
25 low = mid + 1;
26 } else {
27 high = mid - 1;
28 }
29 }
30 return result;
31 }
32
33 public static void main(String[] args) {
34 int[] heights = {4, 12, 2, 7, 3, 18, 20, 3, 19};
35 System.out.println(furthestBuilding(heights, 10, 2)); // Output: 7
36 }
37}
38
In this Java implementation, binary search pinpoints the maximum reachable building by leveraging a helper function `canReach`. It efficiently simulates using available resources at each midpoint index in the search space, providing insight into feasible building reach.