Skip to content

Commit a6d97a3

Browse files
authored
Create 0502-ipo.kt
1 parent dc65ba6 commit a6d97a3

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

kotlin/0502-ipo.kt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
fun findMaximizedCapital(k: Int, w: Int, profits: IntArray, capital: IntArray): Int {
3+
4+
var totalCap = w
5+
// Pair of ("Profit" to "CapitalCost")
6+
val minCapital = PriorityQueue<Pair<Int, Int>> { a,b -> a.second - b.second }
7+
val maxProfit = PriorityQueue<Int> { a,b -> b - a }
8+
9+
profits.zip(capital).forEach {
10+
minCapital.add(it.first to it.second)
11+
}
12+
13+
repeat(k) exit@ {
14+
while(minCapital.isNotEmpty() && minCapital.peek().second <= totalCap) {
15+
val (prof, cap) = minCapital.poll()
16+
maxProfit.add(prof)
17+
}
18+
if(maxProfit.isEmpty()) return@exit
19+
totalCap += maxProfit.poll()
20+
}
21+
22+
return totalCap
23+
}
24+
}

0 commit comments

Comments
 (0)