forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0502-ipo.kt
24 lines (20 loc) · 781 Bytes
/
0502-ipo.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
fun findMaximizedCapital(k: Int, w: Int, profits: IntArray, capital: IntArray): Int {
var totalCap = w
// Pair of ("Profit" to "CapitalCost")
val minCapital = PriorityQueue<Pair<Int, Int>> { a,b -> a.second - b.second }
val maxProfit = PriorityQueue<Int> { a,b -> b - a }
profits.zip(capital).forEach {
minCapital.add(it.first to it.second)
}
repeat(k) exit@ {
while(minCapital.isNotEmpty() && minCapital.peek().second <= totalCap) {
val (prof, cap) = minCapital.poll()
maxProfit.add(prof)
}
if(maxProfit.isEmpty()) return@exit
totalCap += maxProfit.poll()
}
return totalCap
}
}