Keep creating , Accelerate growth ! This is my participation 「 Nuggets day new plan · 6 Yuegengwen challenge 」 Of the 29 God , Click to see the event details
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.While moving from building i to building i+1 (0-indexed),
Return the furthest building index (0-indexed) you can reach if you use the given ladders and bricks optimally.
Example 1:
Input: heights = [4,2,7,6,9,14,12], bricks = 5, ladders = 1
Output: 4
Explanation: Starting at building 0, you can follow these steps:
- Go to building 1 without using ladders nor bricks since 4 >= 2.
- Go to building 2 using 5 bricks. You must use either bricks or ladders because 2 < 7.
- Go to building 3 without using ladders nor bricks since 7 >= 6.
- Go to building 4 using your only ladder. You must use either bricks or ladders because 6 < 9.
It is impossible to go beyond building 4 because you do not have any more bricks or ladders.
Example 2:
Input: heights = [4,12,2,7,3,18,20,3,19], bricks = 10, ladders = 2
Output: 7
Example 3:
Input: heights = [14,3,19,3], bricks = 17, ladders = 0
Output: 3
Note:
1 <= heights.length <= 10^5
1 <= heights[i] <= 10^6
0 <= bricks <= 10^9
0 <= ladders <= heights.length
According to the meaning , Given an array of integers heights, Indicates the height of the building , And some bricks and some ladders . from 0 Building No. 1 starts to walk , And may use bricks or ladders to move to the next building . From architecture i Move to building i+1(0 Indexes ) when ,
If the given ladder and bricks are used in the best way , The farthest building index that can be reached is returned (0 Start index ).
This question is about greedy thinking and sorting , When we move on the roof , You only need to use bricks or ladders when climbing high , Because the ladder is infinitely high , So according to the greedy thought , You must try to use bricks first , But when the bricks are used up , What we need to pay attention to , At this point, if we have no bricks in our hands and only one ladder , It must stop when you run into a tall building after you use it , But if we use a ladder to replace the bricks where we used the most bricks before , We will have many more bricks in our hands , And then we will encounter high-rise buildings within our capabilities , We can still move on , This operation can make the climbing process go further . The most common idea of using bricks here is to use the big root pile .
The time complexity is O(NlogN) , The space complexity is O(N).
class Solution(object):
def furthestBuilding(self, heights, bricks, ladders):
"""
:type heights: List[int]
:type bricks: int
:type ladders: int
:rtype: int
"""
L = []
for i in range(1, len(heights)):
d = heights[i] - heights[i-1]
if d > 0:
heapq.heappush(L, -d)
bricks -= d
if bricks < 0:
if ladders > 0:
ladders -= 1
bricks += -heapq.heappop(L)
else:
return i-1
return len(heights)-1
Runtime: 1035 ms, faster than 34.48% of Python online submissions for Furthest Building You Can Reach.
Memory Usage: 24.3 MB, less than 22.41% of Python online submissions for Furthest Building You Can Reach.
leetcode.com/problems/fu…
Your support is my greatest motivation