nikunjsoni

1642

May 19th, 2021
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.80 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int furthestBuilding(vector<int>& heights, int bricks, int ladders) {
  4.         priority_queue<int, vector<int>, greater<int>> pq;  
  5.        
  6.         for(int i=0; i<heights.size()-1; i++){
  7.             // Get next climb height.
  8.             int climb = heights[i+1]-heights[i];
  9.            
  10.             // If it is down jump, continue.
  11.             if(climb <= 0) continue;
  12.            
  13.             // Assign a ladder to it.
  14.             pq.push(climb);
  15.             if(pq.size() <= ladders) continue;
  16.            
  17.             // If ladders are over, assign bricks to smallest ladder so far.
  18.             int lastClimb = pq.top();
  19.             pq.pop();
  20.             bricks -= lastClimb;
  21.             if(bricks <  0) return i;
  22.         }
  23.         return heights.size()-1;
  24.     }
  25. };
Advertisement
Add Comment
Please, Sign In to add comment