Skip to content

Commit 849c6ad

Browse files
committed
Inproved 502
1 parent 30a024f commit 849c6ad

File tree

1 file changed

+36
-36
lines changed

1 file changed

+36
-36
lines changed

src/main/ts/g0501_0600/s0502_ipo/solution.ts

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,85 +2,85 @@
22
// #2025_04_15_Time_193_ms_(89.19%)_Space_102.47_MB_(8.11%)
33

44
class MaxHeap {
5-
private heap: number[];
5+
private heap: number[]
66

77
constructor() {
8-
this.heap = [];
8+
this.heap = []
99
}
1010

1111
push(value: number) {
12-
this.heap.push(value);
13-
this.heapifyUp();
12+
this.heap.push(value)
13+
this.heapifyUp()
1414
}
1515

1616
pop(): number | undefined {
1717
if (this.heap.length === 0) {
18-
return undefined;
18+
return undefined
1919
}
2020
if (this.heap.length === 1) {
21-
return this.heap.pop();
21+
return this.heap.pop()
2222
}
23-
const max = this.heap[0];
24-
this.heap[0] = this.heap.pop()!;
25-
this.heapifyDown();
26-
return max;
23+
const max = this.heap[0]
24+
this.heap[0] = this.heap.pop()!
25+
this.heapifyDown()
26+
return max
2727
}
2828

2929
isEmpty(): boolean {
30-
return this.heap.length === 0;
30+
return this.heap.length === 0
3131
}
3232

3333
private heapifyUp() {
34-
let index = this.heap.length - 1;
34+
let index = this.heap.length - 1
3535
while (index > 0) {
36-
let parentIndex = Math.floor((index - 1) / 2);
36+
let parentIndex = Math.floor((index - 1) / 2)
3737
if (this.heap[parentIndex] >= this.heap[index]) {
38-
break;
38+
break
3939
}
40-
[this.heap[parentIndex], this.heap[index]] = [this.heap[index], this.heap[parentIndex]];
41-
index = parentIndex;
40+
;[this.heap[parentIndex], this.heap[index]] = [this.heap[index], this.heap[parentIndex]]
41+
index = parentIndex
4242
}
4343
}
4444

4545
private heapifyDown() {
46-
let index = 0;
46+
let index = 0
4747
while (true) {
48-
let left = 2 * index + 1;
49-
let right = 2 * index + 2;
50-
let largest = index;
48+
let left = 2 * index + 1
49+
let right = 2 * index + 2
50+
let largest = index
5151
if (left < this.heap.length && this.heap[left] > this.heap[largest]) {
52-
largest = left;
52+
largest = left
5353
}
5454
if (right < this.heap.length && this.heap[right] > this.heap[largest]) {
55-
largest = right;
55+
largest = right
5656
}
57-
if (largest === index) break;
58-
[this.heap[index], this.heap[largest]] = [this.heap[largest], this.heap[index]];
59-
index = largest;
57+
if (largest === index) break
58+
;[this.heap[index], this.heap[largest]] = [this.heap[largest], this.heap[index]]
59+
index = largest
6060
}
6161
}
6262
}
6363

6464
function findMaximizedCapital(k: number, w: number, profits: number[], capital: number[]): number {
65-
let projects: [number, number][] = [];
66-
const n = profits.length;
65+
let projects: [number, number][] = []
66+
const n = profits.length
6767
for (let i = 0; i < n; i++) {
68-
projects.push([capital[i], profits[i]]);
68+
projects.push([capital[i], profits[i]])
6969
}
70-
projects.sort((a, b) => a[0] - b[0]);
71-
const maxHeap = new MaxHeap();
72-
let i = 0;
70+
projects.sort((a, b) => a[0] - b[0])
71+
const maxHeap = new MaxHeap()
72+
let i = 0
7373
while (k--) {
7474
while (i < n && projects[i][0] <= w) {
75-
maxHeap.push(projects[i][1]);
76-
i++;
75+
maxHeap.push(projects[i][1])
76+
i++
7777
}
7878
if (maxHeap.isEmpty()) {
79-
break;
79+
break
8080
}
81-
w += maxHeap.pop()!;
81+
w += maxHeap.pop()!
8282
}
83-
return w;
83+
return w
8484
}
8585

8686
export { findMaximizedCapital }

0 commit comments

Comments
 (0)