-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0502-ipo.js
More file actions
110 lines (97 loc) · 3.18 KB
/
0502-ipo.js
File metadata and controls
110 lines (97 loc) · 3.18 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
* Ipo
* Time Complexity: O((N + K) log N)
* Space Complexity: O(N)
*/
var findMaximizedCapital = function (k, w, profits, capital) {
const totalProjectsCount = profits.length;
const projectDetails = new Array(totalProjectsCount);
for (
let projectCreationCounter = 0;
projectCreationCounter < totalProjectsCount;
projectCreationCounter++
) {
projectDetails[projectCreationCounter] = [
capital[projectCreationCounter],
profits[projectCreationCounter],
];
}
projectDetails.sort((firstItem, secondItem) => firstItem[0] - secondItem[0]);
const maxProfitPriorityQueue = [];
function addProfitToQueue(incomingProfit) {
maxProfitPriorityQueue.push(incomingProfit);
let currentPosition = maxProfitPriorityQueue.length - 1;
while (currentPosition > 0) {
const parentPosition = Math.floor((currentPosition - 1) / 2);
if (
maxProfitPriorityQueue[parentPosition] >=
maxProfitPriorityQueue[currentPosition]
) {
break;
}
let tempSwap = maxProfitPriorityQueue[parentPosition];
maxProfitPriorityQueue[parentPosition] =
maxProfitPriorityQueue[currentPosition];
maxProfitPriorityQueue[currentPosition] = tempSwap;
currentPosition = parentPosition;
}
}
function extractMaxProfit() {
if (maxProfitPriorityQueue.length === 0) {
return null;
}
const extractedTop = maxProfitPriorityQueue[0];
const lastElementValue = maxProfitPriorityQueue.pop();
if (maxProfitPriorityQueue.length > 0) {
maxProfitPriorityQueue[0] = lastElementValue;
let rootIndex = 0;
while (true) {
let leftChildrenIndex = 2 * rootIndex + 1;
let rightChildrenIndex = 2 * rootIndex + 2;
let maxChildIndex = rootIndex;
if (
leftChildrenIndex < maxProfitPriorityQueue.length &&
maxProfitPriorityQueue[leftChildrenIndex] >
maxProfitPriorityQueue[maxChildIndex]
) {
maxChildIndex = leftChildrenIndex;
}
if (
rightChildrenIndex < maxProfitPriorityQueue.length &&
maxProfitPriorityQueue[rightChildrenIndex] >
maxProfitPriorityQueue[maxChildIndex]
) {
maxChildIndex = rightChildrenIndex;
}
if (maxChildIndex === rootIndex) {
break;
}
let anotherTempSwap = maxProfitPriorityQueue[rootIndex];
maxProfitPriorityQueue[rootIndex] =
maxProfitPriorityQueue[maxChildIndex];
maxProfitPriorityQueue[maxChildIndex] = anotherTempSwap;
rootIndex = maxChildIndex;
}
}
return extractedTop;
}
let projectReadPointer = 0;
let remainingProjectsToPick = k;
let currentCapital = w;
while (remainingProjectsToPick > 0) {
while (
projectReadPointer < totalProjectsCount &&
projectDetails[projectReadPointer][0] <= currentCapital
) {
addProfitToQueue(projectDetails[projectReadPointer][1]);
projectReadPointer++;
}
if (maxProfitPriorityQueue.length === 0) {
return currentCapital;
}
let pickedProfit = extractMaxProfit();
currentCapital += pickedProfit;
remainingProjectsToPick--;
}
return currentCapital;
};