-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFurthestBuildingYouCanReach.cpp
38 lines (38 loc) · 1.45 KB
/
FurthestBuildingYouCanReach.cpp
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
31
32
33
34
35
36
37
38
class Solution {
public:
int furthestBuilding(vector<int>& heights, int bricks, int ladders) {
priority_queue<int, vector<int>, greater<int>> used_ladders;
int ans = 0;
for (int i = 0, diff; i < heights.size()-1; ++i, ++ans) {
diff = heights[i+1]-heights[i];
// Need to use ladder/bricks.
if (diff > 0) {
// Use ladders greedily.
if (ladders > 0) {
used_ladders.push(diff);
--ladders;
continue;
} else if (!used_ladders.empty()) {
int smallest_height = used_ladders.top();
// Also need to check if replacing a ladder is the best option!
if (bricks >= smallest_height && diff > smallest_height) {
// Replace smallest height with bricks.
bricks -= smallest_height;
// Replace the ladder for this height.
used_ladders.pop();
used_ladders.push(diff);
continue;
}
}
// Can't do anything with ladders, try bricks.
if (bricks >= diff) {
bricks -= diff;
continue;
}
// Welp, we can't do anything!
break;
}
}
return ans;
}
};