<p>You are given an integer array <code>heights</code> representing the heights of buildings, some <code>bricks</code>, and some <code>ladders</code>.</p>

<p>You start your journey from building <code>0</code> and move to the next building by possibly using bricks or ladders.</p>

<p>While moving from building <code>i</code> to building <code>i+1</code> (<strong>0-indexed</strong>),</p>

<ul>
	<li>If the current building&#39;s height is <strong>greater than or equal</strong> to the next building&#39;s height, you do <strong>not</strong> need a ladder or bricks.</li>
	<li>If the current building&#39;s height is <b>less than</b> the next building&#39;s height, you can either use <strong>one ladder</strong> or <code>(h[i+1] - h[i])</code> <strong>bricks</strong>.</li>
</ul>

<p><em>Return the furthest building index (0-indexed) you can reach if you use the given ladders and bricks optimally.</em></p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/10/27/q4.gif" style="width: 562px; height: 561px;" />
<pre>
<strong>Input:</strong> heights = [4,2,7,6,9,14,12], bricks = 5, ladders = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> Starting at building 0, you can follow these steps:
- Go to building 1 without using ladders nor bricks since 4 &gt;= 2.
- Go to building 2 using 5 bricks. You must use either bricks or ladders because 2 &lt; 7.
- Go to building 3 without using ladders nor bricks since 7 &gt;= 6.
- Go to building 4 using your only ladder. You must use either bricks or ladders because 6 &lt; 9.
It is impossible to go beyond building 4 because you do not have any more bricks or ladders.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> heights = [4,12,2,7,3,18,20,3,19], bricks = 10, ladders = 2
<strong>Output:</strong> 7
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> heights = [14,3,19,3], bricks = 17, ladders = 0
<strong>Output:</strong> 3
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= heights.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= heights[i] &lt;= 10<sup>6</sup></code></li>
	<li><code>0 &lt;= bricks &lt;= 10<sup>9</sup></code></li>
	<li><code>0 &lt;= ladders &lt;= heights.length</code></li>
</ul>

class Solution {
public:

    static bool comp(pair<int,int> entry1, pair<int,int> entry2)
    {
        return entry1.first < entry2.first;
    }
    int furthestBuilding(vector<int>& heights, int bricks, int ladders) 
    {
        if (ladders >= heights.size() -1 )
            return heights.size() -1 ;

        vector<pair<int,int>> sorted;
        for (int i = 1 ; i < heights.size() ; i++)
        {
            if (heights[i] - heights[i-1] > 0)
            {
                sorted.push_back(make_pair(heights[i] - heights[i-1], i));
            }
        }

        sort(sorted.begin(), sorted.end(), comp);
        return bs(sorted, ladders, bricks, heights);
    }


    bool canReach(vector<pair<int,int>>& sortedClimbs, int ladders, int bricks, int building)
    {
        bool ret = true;

        for (int i = 0 ; i < sortedClimbs.size() ; i++)
        {
            if (sortedClimbs[i].second <= building)
            {    
                if (bricks >= sortedClimbs[i].first)
                {
                    bricks -= sortedClimbs[i].first;
                    continue;
                }

                else if (ladders > 0)
                {
                    ladders--;
                }

                else
                {
                    return false;
                }
            }
        }

        return true;
    }

    int bs(vector<pair<int,int>> &sorted_climbs, int ladders, int bricks, vector<int> &heights)
    {
        int left = 0;
        int right = heights.size()-1;
        int mid = left + (right - left)/2;

        while (left < right)
        {
            bool reachable = canReach(sorted_climbs, ladders, bricks, mid);

            if (reachable)
            {
                left = mid;
            }

            else
            {
                right = mid-1;
            }

            mid = left + (right - left+1)/2;
        }

        return mid;
    }


    bool reachable(vector<int>& heights, int ladders, int bricks, int building)
    {
        vector<int> climbs;
        for (int i = 0 ; i < building ; i++)
        {
            int climb = heights[i+1] - heights[i];
            if (climb > 0)
            {
                climbs.push_back(climb);
            }
        }

        sort(climbs.begin(), climbs.end());

        for (int i = 0; i < climbs.size(); i++)
        {
            if (bricks >= climbs[i])
                bricks -= climbs[i];

            else if (ladders > 0)
                ladders--;

            else
            {
                return false;
            }
        }

        return true;
    }

    int binary(vector<int> &heights, int ladders, int bricks)
    {
        int start = 0;
        int end = heights.size() - 1;
        int mid = (start + end) / 2;

        while (start < end)
        {
            mid = start + (end - start + 1) / 2;
            if (reachable(heights, ladders, bricks, mid))
            {
                start = mid;
            }

            else
            {
                end = mid - 1;
            }
        }

        return end;
    }

    int allocLadders(vector<int> &heights, int ladders, int bricks)
    {
  
        // Climb Height 
        priority_queue<int> climbs;
        for (int i = 0 ; i < heights.size() -1 ; i++ )
        {
            int climb = heights[i+1] - heights[i];

            if (climb <= 0)
            {
                continue;
            }

            climbs.push(-climb);

            if (climbs.size() <= ladders)
            {
                continue;
            }

            bricks -= abs(climbs.top());
            climbs.pop();

            if (bricks<0)
                return i;
            
        }

        return heights.size() -1;
    }

    int allocBricks(vector<int> &heights, int ladders, int bricks)
    {
        priority_queue<int> climbs;
        for (int i = 0 ; i < heights.size() -1 ; i++)
        {
            int climb = heights[i+1] - heights[i];

            if (climb <= 0)
                continue;

            climbs.push(climb);
            bricks -= climb;

            if (bricks >= 0)
                continue;

            if (ladders <= 0)
                return i;

            bricks += climbs.top();
            climbs.pop();
            ladders--;
        }

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