Skip to content

Latest commit

 

History

History
25 lines (21 loc) · 732 Bytes

File metadata and controls

25 lines (21 loc) · 732 Bytes
int furthestBuilding(vector<int>& heights, int bricks, int ladders) {
        priority_queue<int,vector<int>,greater<int>> p;
        int n = heights.size();

        for (int i = 0; i < n - 1; i++) {
            int diff = heights[i + 1] - heights[i];

            if (diff > 0) {
                p.push(diff);

                if (p.size() > ladders) {
                    bricks -= p.top();
                    p.pop();
                    if (bricks < 0) {
                        return i;
                    }
                }
            }
        }
        return n - 1;
    }