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.
1function furthestBuilding(heights, bricks, ladders) {
2 const minHeap = [];
3 function heapPush(val) {
4 minHeap.push(val);
5 minHeap.sort((a, b) => a - b);
6 }
7 function heapPop() {
8 return minHeap.shift();
9 }
10 for (let i = 0; i < heights.length - 1; i++) {
11 let diff = heights[i + 1] - heights[i];
12 if (diff > 0) {
13 heapPush(diff);
14 if (minHeap.length > ladders) {
15 bricks -= heapPop();
16 if (bricks < 0) return i;
17 }
18 }
19 }
20 return heights.length - 1;
21}
22
23// Example usage
24console.log(furthestBuilding([4, 2, 7, 6, 9, 14, 12], 5, 1)); // Output: 4
25
This JavaScript solution simulates a min-heap using an array with sorting operations. It pushes height differences into the array, sorts, and uses minimal climbs when the ladder capacity is surpassed, demonstrating efficient climb management like heaps in other languages.
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.
1#include <iostream>
2#include <queue>
3using namespace std;
4
5bool canReach(vector<int>& heights, int bricks, int ladders, int target) {
6 priority_queue<int, vector<int>, greater<int>> minHeap;
7 for (int i = 0; i < target; ++i) {
8 int diff = heights[i + 1] - heights[i];
9 if (diff > 0) {
10 minHeap.push(diff);
11 if (minHeap.size() > ladders) {
12 bricks -= minHeap.top();
13 minHeap.pop();
14 if (bricks < 0) return false;
15 }
16 }
17 }
18 return true;
19}
20
21int furthestBuilding(vector<int>& heights, int bricks, int ladders) {
22 int low = 0, high = heights.size() - 1, result = 0;
23 while (low <= high) {
24 int mid = low + (high - low) / 2;
25 if (canReach(heights, bricks, ladders, mid)) {
26 result = mid;
27 low = mid + 1;
28 } else {
29 high = mid - 1;
30 }
31 }
32 return result;
33}
34
35int main() {
36 vector<int> heights = {4, 12, 2, 7, 3, 18, 20, 3, 19};
37 cout << furthestBuilding(heights, 10, 2) << endl; // Output: 7
38}
39
This C++ solution leverages binary search combined with `canReach`, a helper function that simulates resource allocation using a min-heap strategy. The binary search efficiently homes in on the furthest building index that can be achieved using available resources by confirming feasibility for each midpoint.