-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy path1642.可以到达的最远建筑.cpp
51 lines (47 loc) · 1020 Bytes
/
1642.可以到达的最远建筑.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
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
* @lc app=leetcode.cn id=1642 lang=cpp
*
* [1642] 可以到达的最远建筑
*/
// @lc code=start
class Solution
{
public:
int furthestBuilding(vector<int> &heights, int bricks, int ladders)
{
if (heights.size() <= 0)
{
return -1;
}
std::priority_queue<int> p;
int sum = 0;
int last = heights[0];
int i = 1;
for (; i < heights.size(); ++i)
{
auto delta = heights[i] - last;
last = heights[i];
if (delta <= 0)
{
continue;
}
p.push(delta);
sum += delta;
if (sum > bricks)
{
if (ladders > 0)
{
--ladders;
sum -= p.top();
p.pop();
}
else
{
break;
}
}
}
return i - 1;
}
};
// @lc code=end