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.
1using System;
2using System.Collections.Generic;
3
4public class FurthestBuilding {
5 public static int FurthestBuildingMethod(int[] heights, int bricks, int ladders) {
6 var minHeap = new SortedSet<(int climb, int index)>(Comparer<(int, int)>.Create((x, y) => x.climb == y.climb ? x.index.CompareTo(y.index) : x.climb.CompareTo(y.climb)));
7 for (int i = 0; i < heights.Length - 1; i++) {
8 int diff = heights[i + 1] - heights[i];
9 if (diff > 0) {
10 minHeap.Add((diff, i));
11 if (minHeap.Count > ladders) {
12 bricks -= minHeap.Min.climb;
13 minHeap.Remove(minHeap.Min);
14 if (bricks < 0) return i;
15 }
16 }
17 }
18 return heights.Length - 1;
19 }
20
21 public static void Main() {
22 int[] heights = { 4, 12, 2, 7, 3, 18, 20, 3, 19 };
23 Console.WriteLine(FurthestBuildingMethod(heights, 10, 2)); // Output: 7
24 }
25}
26
This C# code uses a SortedSet to simulate a min-heap by storing tuples of climb amounts and indices, ensuring optimal usage of bricks and ladders by always retrieving the smallest required resources to progress further, exactly mimicking heap operations available 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 <stdio.h>
2#include <stdlib.h>
3
4int canReach(int* heights, int heightsSize, int bricks, int ladders, int target) {
5 int* minHeap = (int*)malloc(sizeof(int) * heightsSize);
6 int heapSize = 0;
7 for (int i = 0; i < target; ++i) {
8 int diff = heights[i + 1] - heights[i];
9 if (diff > 0) {
10 minHeap[heapSize++] = diff;
11 for (int j = heapSize - 1; j > 0 && minHeap[j] < minHeap[j - 1]; --j) {
12 int temp = minHeap[j];
13 minHeap[j] = minHeap[j - 1];
14 minHeap[j - 1] = temp;
15 }
16 if (heapSize > ladders) {
17 bricks -= minHeap[--heapSize];
18 if (bricks < 0) {
19 free(minHeap);
20 return 0;
21 }
22 }
23 }
24 }
25 free(minHeap);
26 return 1;
27}
28
29int furthestBuilding(int* heights, int heightsSize, int bricks, int ladders) {
30 int low = 0, high = heightsSize - 1, result = 0;
31 while (low <= high) {
32 int mid = (low + high) / 2;
33 if (canReach(heights, heightsSize, bricks, ladders, mid)) {
34 result = mid;
35 low = mid + 1;
36 } else {
37 high = mid - 1;
38 }
39 }
40 return result;
41}
42
43int main() {
44 int heights[] = {4, 2, 7, 6, 9, 14, 12};
45 int result = furthestBuilding(heights, 7, 5, 1);
46 printf("%d\n", result); // Output: 4
47 return 0;
48}
49
In this C code, a binary search is conducted over the range of buildings. For each midpoint, a helper function 'canReach' checks the viability of reaching that point by exploiting a simulated min-heap strategy. This allows optimal usage of the available bricks and ladders, guiding the binary search to converge on the maximum reachable building index.