Posted on

Description

Submission

class Solution {
public:
    int furthestBuilding(vector<int>& heights, int bricks, int ladders) {
        multiset<int> Set;

        for(int i = 1; i < heights.size(); ++i) {
            int d = heights[i] - heights[i-1];
            if(d <= 0) continue;

            if(ladders > 0) {
                ladders -= 1;
                Set.insert(d);
            } else {
                Set.insert(d);
                int r = *Set.begin();
                if(r <= bricks) {
                    bricks -= r;
                    Set.erase(Set.begin());
                } else {
                    return i - 1;
                }
                
            }
        }

        return heights.size() - 1;
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *