-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path215. Kth Largest Element in an Array.ts
137 lines (107 loc) · 3.19 KB
/
215. Kth Largest Element in an Array.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
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
{
// 1. sorting을 이용한 풀이
/**
* Runtime: 117 ms, faster than 53.68% of TypeScript online submissions for Kth Largest Element in an Array.
* Memory Usage: 44.5 MB, less than 92.65% of TypeScript online submissions for Kth Largest Element in an Array.
*/
const findKthLargest = (nums: number[], k: number): number => {
return nums.sort((a, b) => b - a)[k - 1];
};
}
{
// 2. 우선순위 큐(Max Heap) 자료구조를 이용한 풀이
/**
* Runtime: 192 ms, faster than 13.61% of TypeScript online submissions for Kth Largest Element in an Array.
* Memory Usage: 50 MB, less than 5.52% of TypeScript online submissions for Kth Largest Element in an Array.
*/
interface MaxHeapImpl<T> {
push(value: T): void;
pop(): null | T;
peak(): null | T;
}
class Node<T> {
value: T;
next: Node<T> | null;
constructor(value: T) {
this.value = value;
this.next = null;
}
}
class MaxHeap<T> implements MaxHeapImpl<T>{
private _values: T[];
constructor() {
this._values = [];
}
private swap(index1: number, index2: number) {
[this._values[index1], this._values[index2]] = [this._values[index2], this._values[index1]]
}
private parent(index: number) {
return Math.floor((index - 1) / 2);
}
private leftChild(index: number) {
return index * 2 + 1;
}
private rightChild(index: number) {
return index * 2 + 2;
}
private isLeaf(index: number) {
return index <= this._values.length - 1 && index >= Math.floor(this._values.length / 2);
}
push(value: T) {
this._values.push(value);
this.heapifyUp(this._values.length - 1);
}
private heapifyUp(index: number) {
let currentIndex = index;
let parentIndex = this.parent(index);
while (parentIndex >= 0 && this._values[parentIndex] < this._values[currentIndex]) {
this.swap(parentIndex, currentIndex);
currentIndex = parentIndex;
parentIndex = this.parent(currentIndex);
}
}
pop() {
if (this._values.length < 1) {
return null;
}
const returnValue = this._values[0];
this._values[0] = this._values.pop()!;
this.heapifyDown(0);
return returnValue;
}
private heapifyDown(index: number) {
if (this.isLeaf(index)) {
return;
}
let currentIndex = index;
let leftChild = this.leftChild(index);
let rightChild = this.rightChild(index);
if (this._values[currentIndex] < this._values[leftChild]) {
currentIndex = leftChild;
}
if (this._values[currentIndex] < this._values[rightChild]) {
currentIndex = rightChild;
}
if (currentIndex !== index) {
this.swap(index, currentIndex);
this.heapifyDown(currentIndex);
}
}
peak() {
if (this._values.length < 1) {
return null;
}
return this._values[0];
}
}
const findKthLargest = (nums: number[], k: number): number => {
const maxHeap = new MaxHeap();
for (const num of nums) {
maxHeap.push(num);
}
for (let i = 0; i < k - 1; i++) {
maxHeap.pop();
}
return maxHeap.peak() as number;
};
}