-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy path0502-ipo.ts
More file actions
26 lines (23 loc) · 728 Bytes
/
0502-ipo.ts
File metadata and controls
26 lines (23 loc) · 728 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
function findMaximizedCapital(
k: number,
w: number,
profits: number[],
capital: number[],
): number {
const minCapital = new PriorityQueue({ compare: (a, b) => a[1] - b[1] });
const maxProfit = new MaxPriorityQueue();
for (let i = 0; i < profits.length; i++) {
minCapital.enqueue([profits[i], capital[i]]);
}
let profit = w;
for (let i = 0; i < k; i++) {
while (!minCapital.isEmpty() && minCapital.front()[1] <= profit) {
const element = minCapital.dequeue();
maxProfit.enqueue(element[0]);
}
if (maxProfit.isEmpty()) break;
const { element } = maxProfit.dequeue();
profit += element;
}
return profit;
}