Practice › Heap & Priority Queue
Furthest Building You Can Reach
Medium · Heap & Priority Queue · Asked at Google, Amazon, Microsoft
You walk from building 0 to the right. Going from building i to i + 1 is free if h[i + 1] ≤ h[i]; otherwise you must use either one ladder or h[i + 1] − h[i] bricks. You have b bricks and l ladders. Print the index (0-based) of the furthest building you can reach.
Input
First line: n b l. Second line: the n heights.
Output
The furthest reachable building index.
Constraints
- 1 ≤ n ≤ 10^5
- 0 ≤ b ≤ 10^9
- 0 ≤ l ≤ n
- 1 ≤ h[i] ≤ 10^6
Ladders should go to the biggest climbs. Push every climb into a min-heap; when the heap holds more than l climbs, pay for the smallest one with bricks. Stop when the bricks run out.
Example 1
Input
7 5 1 4 2 7 6 9 14 12
Output
4
Example 2
Input
9 10 2 4 12 2 7 3 18 20 3 19
Output
7
Hints
- A ladder covers any climb, so it is best spent on the largest climbs.
- You do not know the future — but you can change your mind about the past.
- Keep the climbs given ladders in a min-heap; when there are too many, the smallest one is paid with bricks instead.
Approach
Walk left to right. For every climb d > 0, push d into a min-heap (tentatively using a ladder). If the heap now holds more than l climbs, pop the smallest and pay for it with bricks. If the bricks go negative, building i + 1 cannot be reached — answer i. If the walk finishes, the answer is n − 1. At every point the ladders cover the l largest climbs so far, which is optimal.
Complexity: O(n log l) time · O(l) space
Solution
C++ solution
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, l;
long long bricks;
cin >> n >> bricks >> l;
vector<long long> h(n);
for (auto &x : h) cin >> x;
priority_queue<long long, vector<long long>, greater<long long>> pq;
for (int i = 0; i + 1 < n; i++) {
long long d = h[i + 1] - h[i];
if (d <= 0) continue;
pq.push(d);
if ((int) pq.size() > l) {
bricks -= pq.top();
pq.pop();
}
if (bricks < 0) {
cout << i << "\n";
return 0;
}
}
cout << n - 1 << "\n";
return 0;
}Python solution
import heapq
import sys
data = list(map(int, sys.stdin.read().split()))
n, bricks, l = data[0], data[1], data[2]
h = data[3 : 3 + n]
heap = []
answer = n - 1
for i in range(n - 1):
d = h[i + 1] - h[i]
if d <= 0:
continue
heapq.heappush(heap, d)
if len(heap) > l:
bricks -= heapq.heappop(heap)
if bricks < 0:
answer = i
break
print(answer)Java solution
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
in.nextToken();
int n = (int) in.nval;
in.nextToken();
long bricks = (long) in.nval;
in.nextToken();
int l = (int) in.nval;
long[] h = new long[n];
for (int i = 0; i < n; i++) {
in.nextToken();
h[i] = (long) in.nval;
}
PriorityQueue<Long> pq = new PriorityQueue<>();
for (int i = 0; i + 1 < n; i++) {
long d = h[i + 1] - h[i];
if (d <= 0) continue;
pq.add(d);
if (pq.size() > l) bricks -= pq.poll();
if (bricks < 0) {
System.out.println(i);
return;
}
}
System.out.println(n - 1);
}
}Solve Furthest Building You Can Reach online · All practice problems