|
| 1 | +<h2><a href="https://leetcode.com/problems/furthest-building-you-can-reach/">1642. Furthest Building You Can Reach</a></h2><h3>Medium</h3><hr><div><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> |
| 2 | + |
| 3 | +<p>You start your journey from building <code>0</code> and move to the next building by possibly using bricks or ladders.</p> |
| 4 | + |
| 5 | +<p>While moving from building <code>i</code> to building <code>i+1</code> (<strong>0-indexed</strong>),</p> |
| 6 | + |
| 7 | +<ul> |
| 8 | + <li>If the current building's height is <strong>greater than or equal</strong> to the next building's height, you do <strong>not</strong> need a ladder or bricks.</li> |
| 9 | + <li>If the current building's height is <b>less than</b> the next building's height, you can either use <strong>one ladder</strong> or <code>(h[i+1] - h[i])</code> <strong>bricks</strong>.</li> |
| 10 | +</ul> |
| 11 | + |
| 12 | +<p><em>Return the furthest building index (0-indexed) you can reach if you use the given ladders and bricks optimally.</em></p> |
| 13 | + |
| 14 | +<p> </p> |
| 15 | +<p><strong>Example 1:</strong></p> |
| 16 | +<img alt="" src="https://assets.leetcode.com/uploads/2020/10/27/q4.gif" style="width: 562px; height: 561px;"> |
| 17 | +<pre><strong>Input:</strong> heights = [4,2,7,6,9,14,12], bricks = 5, ladders = 1 |
| 18 | +<strong>Output:</strong> 4 |
| 19 | +<strong>Explanation:</strong> Starting at building 0, you can follow these steps: |
| 20 | +- Go to building 1 without using ladders nor bricks since 4 >= 2. |
| 21 | +- Go to building 2 using 5 bricks. You must use either bricks or ladders because 2 < 7. |
| 22 | +- Go to building 3 without using ladders nor bricks since 7 >= 6. |
| 23 | +- Go to building 4 using your only ladder. You must use either bricks or ladders because 6 < 9. |
| 24 | +It is impossible to go beyond building 4 because you do not have any more bricks or ladders. |
| 25 | +</pre> |
| 26 | + |
| 27 | +<p><strong>Example 2:</strong></p> |
| 28 | + |
| 29 | +<pre><strong>Input:</strong> heights = [4,12,2,7,3,18,20,3,19], bricks = 10, ladders = 2 |
| 30 | +<strong>Output:</strong> 7 |
| 31 | +</pre> |
| 32 | + |
| 33 | +<p><strong>Example 3:</strong></p> |
| 34 | + |
| 35 | +<pre><strong>Input:</strong> heights = [14,3,19,3], bricks = 17, ladders = 0 |
| 36 | +<strong>Output:</strong> 3 |
| 37 | +</pre> |
| 38 | + |
| 39 | +<p> </p> |
| 40 | +<p><strong>Constraints:</strong></p> |
| 41 | + |
| 42 | +<ul> |
| 43 | + <li><code>1 <= heights.length <= 10<sup>5</sup></code></li> |
| 44 | + <li><code>1 <= heights[i] <= 10<sup>6</sup></code></li> |
| 45 | + <li><code>0 <= bricks <= 10<sup>9</sup></code></li> |
| 46 | + <li><code>0 <= ladders <= heights.length</code></li> |
| 47 | +</ul> |
| 48 | +</div> |
0 commit comments