Skip to content

Commit 10cfc69

Browse files
authored
Create 1642-furthest-building-you-can-reach.kt
1 parent be87082 commit 10cfc69

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
def furthestBuilding(self, heights: List[int], bricks: int, ladders: int) -> int:
3+
heap = []
4+
5+
for i in range(len(heights) - 1):
6+
diff = heights[i + 1] - heights[i]
7+
if diff <= 0:
8+
continue
9+
10+
bricks -= diff
11+
heapq.heappush(heap, -diff)
12+
13+
if bricks < 0:
14+
if ladders == 0:
15+
return i
16+
ladders -= 1
17+
bricks += -heapq.heappop(heap)
18+
19+
return len(heights) - 1

0 commit comments

Comments
 (0)