Skip to content

Commit e2bc669

Browse files
committed
Solution as on 27-11-2021 10:30 pm
1 parent 42b2bda commit e2bc669

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// 1642.✅ Furthest Building You Can Reach
2+
3+
class Solution
4+
{
5+
public:
6+
int furthestBuilding(vector<int> &heights, int bricks, int ladders)
7+
{
8+
priority_queue<int> pq;
9+
10+
int i = 0;
11+
for (i = 0; i < heights.size() - 1; ++i)
12+
{
13+
int diff = heights[i + 1] - heights[i];
14+
if (diff <= 0)
15+
continue;
16+
bricks -= diff;
17+
pq.push(diff);
18+
if (bricks < 0)
19+
{
20+
bricks += pq.top();
21+
pq.pop();
22+
--ladders;
23+
}
24+
if (ladders < 0)
25+
break;
26+
}
27+
28+
return i;
29+
}
30+
};

0 commit comments

Comments
 (0)