leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
1642.cpp (633B)
0 class Solution { 1 public: 2 int furthestBuilding(const vector<int> &heights, int bricks, int ladders) const { 3 const int n = size(heights); 4 priority_queue<int> pq; 5 6 for (int i = 1; i < n; i++) { 7 const int diff = heights[i] - heights[i - 1]; 8 if (diff <= 0) continue; 9 if (diff > bricks) { 10 if (ladders-- == 0) return i - 1; 11 if (pq.empty() || pq.top() < diff) continue; 12 bricks += pq.top(); 13 pq.pop(); 14 } 15 bricks -= diff; 16 pq.push(diff); 17 } 18 19 return n - 1; 20 } 21 };