leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

1642.cpp (633B)


      1 class Solution {
      2   public:
      3     int furthestBuilding(const vector<int> &heights, int bricks, int ladders) const {
      4         const int n = size(heights);
      5         priority_queue<int> pq;
      6 
      7         for (int i = 1; i < n; i++) {
      8             const int diff = heights[i] - heights[i - 1];
      9             if (diff <= 0) continue;
     10             if (diff > bricks) {
     11                 if (ladders-- == 0) return i - 1;
     12                 if (pq.empty() || pq.top() < diff) continue;
     13                 bricks += pq.top();
     14                 pq.pop();
     15             }
     16             bricks -= diff;
     17             pq.push(diff);
     18         }
     19 
     20         return n - 1;
     21     }
     22 };