<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>

import heapq
class Solution:
    def furthestBuilding(self, heights: List[int], bricks: int, ladders: int) -> int:

        """
        # Backtrack solution
        # T: O(2^n) | TLE
        # Function to return max idx it can reach
        def solve(idx, heights, bricks, ladder):
            if idx == len(heights) - 1:
                return idx

            if t[bricks][ladder] != -1:
                return t[bricks][ladder]

            if heights[idx + 1] <= heights[idx]:
                t[bricks][ladder] = solve(idx + 1, heights, bricks, ladder)
                return t[bricks][ladder]

            # use bricks
            ret1 = idx
            diff = heights[idx + 1] - heights[idx]
            if bricks >= diff:
                ret1 = solve(idx + 1, heights, bricks - diff, ladder)

            # use ladder
            ret2 = idx
            if ladder > 0:
                ret2 = solve(idx + 1, heights, bricks, ladder - 1)

            t[bricks][ladder] = max(ret1, ret2)
            return t[bricks][ladder]



        t = [[-1 for _ in range(ladders + 1)] for _ in range(bricks + 1)]
        return solve(0, heights, bricks, ladders)
        """
        # Hax heap
        heap = []
        i = 0
        while i < len(heights) - 1:
            if heights[i+1] <= heights[i]:
                i += 1
                continue

            diff = heights[i+1] - heights[i]
            # Bricks can be used
            if bricks >= diff:
                bricks -= diff
                heapq.heappush(heap, -diff)
            else:
                 # Bricks can NOT be used, check if ladder can be used
                if ladders <= 0:
                    return i

                # Ladder can be use whether here or in the past
                ladders -= 1

                bricks_used_in_past = 0
                if heap:
                    bricks_used_in_past = -heap[0]

                if bricks_used_in_past > diff:
                    # UNDO : Use bricks now and use ladder in the past

                    # Undo bricks count
                    bricks += bricks_used_in_past

                    # use bricks now
                    bricks -= diff

                    heapq.heappop(heap)
                    # Insert this brick used info in heap
                    heapq.heappush(heap, -diff)
            i += 1
        return len(heights) - 1




        
180