-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIPO.js
More file actions
159 lines (134 loc) · 3.96 KB
/
IPO.js
File metadata and controls
159 lines (134 loc) · 3.96 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
var findMaximizedCapital = function (k, w, profits, capital) {
let projects = [];
let heap = new MaxHeap();
for (let i = 0; i < profits.length; i++) {
projects.push([profits[i], capital[i]]);
}
projects.sort((a, b) => a[1] - b[1]);
let x = 0;
while (projects[x] && projects[x][1] <= w) {
heap.add(projects[x][0]);
x++;
}
while (heap.values.length > 0 && k > 0) {
w += heap.extractMax();
k--;
while (projects[x] && projects[x][1] <= w) {
heap.add(projects[x][0]);
x++;
}
}
return w;
};
class MaxHeap {
constructor() {
this.values = [];
}
// index of the parent node
parent(index) {
return Math.floor((index - 1) / 2);
}
// index of the left child node
leftChild(index) {
return index * 2 + 1;
}
// index of the right child node
rightChild(index) {
return index * 2 + 2;
}
// returns true if index is of a node that has no children
isLeaf(index) {
return (
index >= Math.floor(this.values.length / 2) &&
index <= this.values.length - 1
);
}
// swap using ES6 destructuring
swap(index1, index2) {
[this.values[index1], this.values[index2]] = [
this.values[index2],
this.values[index1],
];
}
heapifyDown(index) {
// if the node at index has children
if (!this.isLeaf(index)) {
// get indices of children
let leftChildIndex = this.leftChild(index),
rightChildIndex = this.rightChild(index),
// start out largest index at parent index
largestIndex = index;
// if the left child > parent
if (this.values[leftChildIndex] > this.values[largestIndex]) {
// reassign largest index to left child index
largestIndex = leftChildIndex;
}
// if the right child > element at largest index (either parent or left child)
if (this.values[rightChildIndex] >= this.values[largestIndex]) {
// reassign largest index to right child index
largestIndex = rightChildIndex;
}
// if the largest index is not the parent index
if (largestIndex !== index) {
// swap
this.swap(index, largestIndex);
// recursively move down the heap
this.heapifyDown(largestIndex);
}
}
}
heapifyUp(index) {
let currentIndex = index,
parentIndex = this.parent(currentIndex);
// while we haven't reached the root node and the current element is greater than its parent node
while (
currentIndex > 0 &&
this.values[currentIndex] > this.values[parentIndex]
) {
// swap
this.swap(currentIndex, parentIndex);
// move up the binary heap
currentIndex = parentIndex;
parentIndex = this.parent(parentIndex);
}
}
add(element) {
// add element to the end of the heap
this.values.push(element);
// move element up until it's in the correct position
this.heapifyUp(this.values.length - 1);
}
// returns value of max without removing
peek() {
return this.values[0];
}
// removes and returns max element
extractMax() {
if (this.values.length === 1) return this.values.pop();
if (this.values.length < 1) return "heap is empty";
// get max and last element
const max = this.values[0];
const end = this.values.pop();
// reassign first element to the last element
this.values[0] = end;
// heapify down until element is back in its correct position
this.heapifyDown(0);
// return the max
return max;
}
buildHeap(array) {
this.values = array;
// since leaves start at floor(nodes / 2) index, we work from the leaves up the heap
for (let i = Math.floor(this.values.length / 2); i >= 0; i--) {
this.heapifyDown(i);
}
}
}
console.log(
findMaximizedCapital(
(k = 2),
(w = 0),
(profits = [1, 2, 3]),
(capital = [0, 1, 1])
)
);