<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:
    //簡單來說,難點在於要決定何時要使用梯子或磚頭
    //如果需要的磚頭不夠多會優先使用磚頭比較划算,但如果會一口氣用掉太多磚頭則梯子比較划算
    //最後就看在最佳化策略之下能走到多遠
    //P.S.忽然發現關於priority_queue幾個重點
    //1.這個priority_queue是minHeap或是maxHeap
    //2.滿足什麼條件的情況下需要pop item? 當滿足條件時,當下的item需要先push進去排序後再pop嗎?
    //3.當pop item的時候需要做什麼事情? 有需要再push item回去嗎?
    //4.當push item的時候有條件限制嗎?
    //5.當push item的時候有需要把資料全都一次push完再處理嗎? 還是可以邊push邊排序,並且邊判斷是否需要pop?
    //  => ans: 通常都是邊push邊排序的處理flow
    //6.push的資料有需要預先排序過嗎? 還是可以不用排序? 
    //  => ans: 通常是根據push資料的優先順序是否會因為資料大小而被影響,如果會就要,如果不會就可以不用
    int furthestBuilding(vector<int>& heights, int bricks, int ladders) {
        int limit = heights.size(), diff, sum = 0, answer = 0;
        vector<pair<int,int>> jumps;
        for(int i = 1; i < limit; ++i) {
            diff = heights[i] - heights[i-1];
            if(diff > 0)
                jumps.push_back({diff,i});
        }

        //1.根據前面的整理,這題很明顯是要用maxHeap
        priority_queue<pair<int,int>> maxHeap;
        for(auto& jump : jumps) {
            sum += jump.first;
            maxHeap.push(jump);
            if(sum > bricks) {
                if(ladders == 0)
                    return jump.second - 1;

                sum -= maxHeap.top().first;
                //answer = jump.second;
                maxHeap.pop();
                --ladders;
            }
        }
        //如果jumps都能克服表示能走到終點,回傳最後一個index
        return limit - 1;
    }
};
67300