Skip to content

Latest commit

 

History

History
31 lines (25 loc) · 805 Bytes

furthest_point_reachable.md

File metadata and controls

31 lines (25 loc) · 805 Bytes

1642. Furthest Building You Can Reach

Implementation
   int furthestBuilding(vector<int>& heights, int bricks, int ladders) {
        int n = heights.size(); 
        priority_queue<int, vector<int>, greater<int>> pq; 
        
        for (int i = 1; i < n; i++) {
            int diff = heights[i] - heights[i - 1];
            if (0 < diff) {
                pq.push(diff);
            }
            if (ladders < pq.size()) {
                bricks -= pq.top(); 
                pq.pop();
            }
            if (bricks < 0) {
                return i - 1;
            }
        }
        return n - 1;
    }