forked from VivekDubey9/Competitive-Programming-Algos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfarthest_buildings.cpp
More file actions
45 lines (41 loc) · 1.01 KB
/
farthest_buildings.cpp
File metadata and controls
45 lines (41 loc) · 1.01 KB
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
39
40
41
42
43
44
45
class Solution {
public:
int furthestBuilding(vector<int>& heights, int br, int lad) {
priority_queue<int,vector<int>,greater<int>> que;
int i=0;
int diff;
while(que.size()<lad && i<heights.size()-1)
{
diff = heights[i+1]-heights[i];
if(diff<=0)
{
i++;
continue;
}
que.push(diff);
i++;
}
for(;i<heights.size()-1;i++)
{
diff = heights[i+1]-heights[i];
if(diff>0)
{
if(!que.empty() && que.top()<diff)
{
br = br - que.top();
que.pop();
que.push(diff);
}
else
{
br = br - diff;
}
}
if(br<0)
{
return i;
}
}
return i;
}
};