Skip to content

Commit be87082

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

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
fun furthestBuilding(heights: IntArray, bricks: Int, ladders: Int): Int {
3+
val maxHeap = PriorityQueue<Int> { a, b -> b - a }
4+
var bricks = bricks
5+
var ladders = ladders
6+
7+
for (i in 0 until heights.lastIndex) {
8+
val diff = heights[i + 1] - heights[i]
9+
if (diff <= 0) continue
10+
11+
bricks = bricks - diff
12+
maxHeap.add(diff)
13+
14+
if (bricks < 0) {
15+
if (ladders == 0) return i
16+
ladders -= 1
17+
bricks += maxHeap.poll()
18+
}
19+
}
20+
21+
return heights.lastIndex
22+
}
23+
}

0 commit comments

Comments
 (0)