3
int furthestBuilding(vector<int> &heights, int bricks, int ladders) {4
// Priority Queue for storing the bricks used in each step in decreasing6
priority_queue<int> maxB;8
int i = 0, diff = 0; // i is used for storing the position and diff for10
for (i = 0; i < heights.size() - 1; i++) { // go till before the last building.12
// difference of the height of corresponding buildings13
diff = heights[i + 1] - heights[i];15
// If next building is equal or samaller than current then go to next21
bricks -= diff; // taking the bricks needed for going to next building.22
maxB.push(diff); // adding the number of bricks used in priority queue.24
// if bricks become negetive then there were not enough bricks. So add a25
// ladder in place of the step where most bricks were used.27
bricks += maxB.top(); // taking back bricks from that step28
maxB.pop(); // As max bricks were removed so pop29
ladders--; // 1 ladder used32
// if ladder is negetive then the ladder was not provided to go to next33
// building. So we can't proceed.34
if (ladders < 0) break;37
// return the present position.