-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path502-IPO.ts
95 lines (75 loc) · 2.59 KB
/
502-IPO.ts
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
function findMaximizedCapital(k: number, w: number, profits: number[], capital: number[]): number {
const n = profits.length
const projects = Array.from({ length: n }, (_, i) => [capital[i], profits[i]])
projects.sort((a, b) => a[0] - b[0])
const maxHeap = new MaxHeap2()
let currentCapital = w
let i = 0
for (let j = 0; j < k; j++) {
while (i < n && projects[i][0] <= currentCapital) {
maxHeap.insert(projects[i][1])
i++
}
const maxProfit = maxHeap.extractMax()
if (maxProfit === undefined) break
currentCapital += maxProfit
}
return currentCapital
}
class MaxHeap2 {
private heap: number[]
constructor() {
this.heap = []
}
public insert(value: number): void {
this.heap.push(value)
this.bubbleUp(this.heap.length - 1)
}
public extractMax(): number | undefined {
if (this.heap.length === 0) return undefined
if (this.heap.length === 1) return this.heap.pop()
const max = this.heap[0]
this.heap[0] = this.heap.pop()!
this.bubbleDown(0)
return max
}
public peek(): number | undefined {
return this.heap[0]
}
private bubbleUp(index: number): void {
while (index > 0) {
const parentIndex = Math.floor((index - 1) / 2)
if (this.heap[index] <= this.heap[parentIndex]) break;
[this.heap[index], this.heap[parentIndex]] = [this.heap[parentIndex], this.heap[index]]
index = parentIndex
}
}
private bubbleDown(index: number): void {
const length = this.heap.length
const element = this.heap[index]
while (true) {
const leftChildIndex = 2 * index + 1
const rightChildIndex = 2 * index + 2
let leftChild, rightChild
let swap: number | undefined
if (leftChildIndex < length) {
leftChild = this.heap[leftChildIndex]
if (leftChild > element) {
swap = leftChildIndex
}
}
if (rightChildIndex < length) {
rightChild = this.heap[rightChildIndex]
if (
(swap === undefined && rightChild > element) ||
(swap !== undefined && rightChild > leftChild!)
) {
swap = rightChildIndex
}
}
if (swap === undefined) break;
[this.heap[index], this.heap[swap]] = [this.heap[swap], this.heap[index]]
index = swap
}
}
}