Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- int furthestBuilding(vector<int>& heights, int bricks, int ladders) {
- priority_queue<int, vector<int>, greater<int>> pq;
- for(int i=0; i<heights.size()-1; i++){
- // Get next climb height.
- int climb = heights[i+1]-heights[i];
- // If it is down jump, continue.
- if(climb <= 0) continue;
- // Assign a ladder to it.
- pq.push(climb);
- if(pq.size() <= ladders) continue;
- // If ladders are over, assign bricks to smallest ladder so far.
- int lastClimb = pq.top();
- pq.pop();
- bricks -= lastClimb;
- if(bricks < 0) return i;
- }
- return heights.size()-1;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment