{"pageProps":{"post":{"title":"1642. Furthest Building You Can Reach","excerpt":"You are given an integer array heights representing the heights of buildings, some bricks, and some ladders. You start your journey from building 0 and move to the next building by possibly using bricks or ladders. Return the furthest building index (0-indexed) you can reach if you use the given ladders and bricks optimally.","featuredImage":{"url":"https://media.graphassets.com/hMPYrFStXjUlOZVXjMgl"},"author":{"name":"Md Jawad Noor Asif","bio":"A humble student of knowledge","photo":{"url":"https://media.graphassets.com/nky5NwkETTKtpNnikulF"}},"createdAt":"2022-06-21T03:59:59.999144+00:00","slug":"furthest-building-you-can-reach","content":{"raw":{"children":[{"type":"heading-one","children":[{"text":"Problem:"}]},{"type":"paragraph","children":[{"text":"You are given an integer array "},{"code":true,"text":"heights"},{"text":" representing the heights of buildings, some "},{"code":true,"text":"bricks"},{"text":", and some "},{"code":true,"text":"ladders"},{"text":"."}]},{"type":"paragraph","children":[{"text":"You start your journey from building "},{"code":true,"text":"0"},{"text":" and move to the next building by possibly using bricks or ladders."}]},{"type":"paragraph","children":[{"text":"While moving from building "},{"code":true,"text":"i"},{"text":" to building "},{"code":true,"text":"i+1"},{"text":" ("},{"bold":true,"text":"0-indexed"},{"text":"),"}]},{"type":"bulleted-list","children":[{"type":"list-item","children":[{"type":"list-item-child","children":[{"text":"If the current building's height is "},{"bold":true,"text":"greater than or equal"},{"text":" to the next building's height, you do "},{"bold":true,"text":"not"},{"text":" need a ladder or bricks."}]}]},{"type":"list-item","children":[{"type":"list-item-child","children":[{"text":"If the current building's height is less than the next building's height, you can either use "},{"bold":true,"text":"one ladder"},{"text":" or "},{"code":true,"text":"(h[i+1] - h[i])"},{"text":" "},{"bold":true,"text":"bricks"},{"text":"."}]}]}]},{"type":"paragraph","children":[{"text":"Return the furthest building index (0-indexed) you can reach if you use the given ladders and bricks optimally.","italic":true}]},{"type":"paragraph","children":[{"text":" "}]},{"type":"paragraph","children":[{"bold":true,"text":"Example 1:"}]},{"type":"paragraph","children":[{"text":""}]},{"src":"https://media.graphassets.com/3bLAhmq8RO2FDF4g1BVB","type":"image","title":"q4.gif","width":562,"handle":"3bLAhmq8RO2FDF4g1BVB","height":561,"children":[{"text":""}],"mimeType":"image/gif"},{"type":"code-block","children":[{"bold":true,"text":"Input:"},{"text":" heights = [4,2,7,6,9,14,12], bricks = 5, ladders = 1\n"},{"bold":true,"text":"Output:"},{"text":" 4\n"},{"bold":true,"text":"Explanation:"},{"text":" Starting at building 0, you can follow these steps:\n- Go to building 1 without using ladders nor bricks since 4 >= 2.\n- Go to building 2 using 5 bricks. You must use either bricks or ladders because 2 < 7.\n- Go to building 3 without using ladders nor bricks since 7 >= 6.\n- Go to building 4 using your only ladder. You must use either bricks or ladders because 6 < 9.\nIt is impossible to go beyond building 4 because you do not have any more bricks or ladders.\n"}]},{"type":"paragraph","children":[{"bold":true,"text":"Example 2:"}]},{"type":"code-block","children":[{"bold":true,"text":"Input:"},{"text":" heights = [4,12,2,7,3,18,20,3,19], bricks = 10, ladders = 2\n"},{"bold":true,"text":"Output:"},{"text":" 7\n"}]},{"type":"paragraph","children":[{"bold":true,"text":"Example 3:"}]},{"type":"code-block","children":[{"bold":true,"text":"Input:"},{"text":" heights = [14,3,19,3], bricks = 17, ladders = 0\n"},{"bold":true,"text":"Output:"},{"text":" 3\n"}]},{"type":"paragraph","children":[{"text":" "}]},{"type":"paragraph","children":[{"bold":true,"text":"Constraints:"}]},{"type":"bulleted-list","children":[{"type":"list-item","children":[{"type":"list-item-child","children":[{"code":true,"text":"1 <= heights.length <= 105"}]}]},{"type":"list-item","children":[{"type":"list-item-child","children":[{"code":true,"text":"1 <= heights[i] <= 106"}]}]},{"type":"list-item","children":[{"type":"list-item-child","children":[{"code":true,"text":"0 <= bricks <= 109"}]}]},{"type":"list-item","children":[{"type":"list-item-child","children":[{"code":true,"text":"0 <= ladders <= heights.length"}]}]}]},{"type":"paragraph","children":[{"text":""}]},{"type":"heading-one","children":[{"text":"Solution:"}]},{"type":"paragraph","children":[{"text":"For solution, special thanks to "},{"href":"https://leetcode.com/kkg2002","type":"link","children":[{"text":"kkg2002"}],"className":"link__Lpjq","openInNewTab":true},{"text":""}]},{"type":"class","children":[{"type":"code-block","children":[{"text":"class Solution:\n def furthestBuilding(self, A, bricks, ladders):\n heap = []\n for i in xrange(len(A) - 1):\n d = A[i + 1] - A[i]\n if d > 0:\n heapq.heappush(heap, d)\n if len(heap) > ladders:\n bricks -= heapq.heappop(heap)\n if bricks < 0:\n return i\n return len(A) - 1"}]}],"className":"python"},{"type":"paragraph","children":[{"text":""}]}]}},"categories":[{"name":"LeetCode","slug":"leetcode"},{"name":"Python","slug":"python"}]}},"__N_SSG":true}