|
2 | 2 | // #2025_04_15_Time_193_ms_(89.19%)_Space_102.47_MB_(8.11%)
|
3 | 3 |
|
4 | 4 | class MaxHeap {
|
5 |
| - private heap: number[]; |
| 5 | + private heap: number[] |
6 | 6 |
|
7 | 7 | constructor() {
|
8 |
| - this.heap = []; |
| 8 | + this.heap = [] |
9 | 9 | }
|
10 | 10 |
|
11 | 11 | push(value: number) {
|
12 |
| - this.heap.push(value); |
13 |
| - this.heapifyUp(); |
| 12 | + this.heap.push(value) |
| 13 | + this.heapifyUp() |
14 | 14 | }
|
15 | 15 |
|
16 | 16 | pop(): number | undefined {
|
17 | 17 | if (this.heap.length === 0) {
|
18 |
| - return undefined; |
| 18 | + return undefined |
19 | 19 | }
|
20 | 20 | if (this.heap.length === 1) {
|
21 |
| - return this.heap.pop(); |
| 21 | + return this.heap.pop() |
22 | 22 | }
|
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 |
27 | 27 | }
|
28 | 28 |
|
29 | 29 | isEmpty(): boolean {
|
30 |
| - return this.heap.length === 0; |
| 30 | + return this.heap.length === 0 |
31 | 31 | }
|
32 | 32 |
|
33 | 33 | private heapifyUp() {
|
34 |
| - let index = this.heap.length - 1; |
| 34 | + let index = this.heap.length - 1 |
35 | 35 | while (index > 0) {
|
36 |
| - let parentIndex = Math.floor((index - 1) / 2); |
| 36 | + let parentIndex = Math.floor((index - 1) / 2) |
37 | 37 | if (this.heap[parentIndex] >= this.heap[index]) {
|
38 |
| - break; |
| 38 | + break |
39 | 39 | }
|
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 |
42 | 42 | }
|
43 | 43 | }
|
44 | 44 |
|
45 | 45 | private heapifyDown() {
|
46 |
| - let index = 0; |
| 46 | + let index = 0 |
47 | 47 | 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 |
51 | 51 | if (left < this.heap.length && this.heap[left] > this.heap[largest]) {
|
52 |
| - largest = left; |
| 52 | + largest = left |
53 | 53 | }
|
54 | 54 | if (right < this.heap.length && this.heap[right] > this.heap[largest]) {
|
55 |
| - largest = right; |
| 55 | + largest = right |
56 | 56 | }
|
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 |
60 | 60 | }
|
61 | 61 | }
|
62 | 62 | }
|
63 | 63 |
|
64 | 64 | 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 |
67 | 67 | for (let i = 0; i < n; i++) {
|
68 |
| - projects.push([capital[i], profits[i]]); |
| 68 | + projects.push([capital[i], profits[i]]) |
69 | 69 | }
|
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 |
73 | 73 | while (k--) {
|
74 | 74 | while (i < n && projects[i][0] <= w) {
|
75 |
| - maxHeap.push(projects[i][1]); |
76 |
| - i++; |
| 75 | + maxHeap.push(projects[i][1]) |
| 76 | + i++ |
77 | 77 | }
|
78 | 78 | if (maxHeap.isEmpty()) {
|
79 |
| - break; |
| 79 | + break |
80 | 80 | }
|
81 |
| - w += maxHeap.pop()!; |
| 81 | + w += maxHeap.pop()! |
82 | 82 | }
|
83 |
| - return w; |
| 83 | + return w |
84 | 84 | }
|
85 | 85 |
|
86 | 86 | export { findMaximizedCapital }
|
0 commit comments