Skip to content

Commit d15926b

Browse files
committed
feat: add js solution to lc problem: No.0703
No.0703.Kth Largest Element in a Stream
1 parent 3b3c49b commit d15926b

File tree

3 files changed

+352
-0
lines changed

3 files changed

+352
-0
lines changed

solution/0700-0799/0703.Kth Largest Element in a Stream/README.md

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,125 @@ public:
140140
*/
141141
```
142142
143+
### **TypeScript**
144+
145+
```js
146+
/**
147+
* @param {number} k
148+
* @param {number[]} nums
149+
*/
150+
var KthLargest = function(k, nums) {
151+
this.k = k;
152+
this.heap = new MinHeap();
153+
for (let num of nums) {
154+
this.add(num);
155+
}
156+
};
157+
158+
/**
159+
* @param {number} val
160+
* @return {number}
161+
*/
162+
KthLargest.prototype.add = function(val) {
163+
this.heap.offer(val);
164+
if (this.heap.size() > this.k) {
165+
this.heap.poll();
166+
}
167+
return this.heap.peek();
168+
};
169+
170+
class MinHeap {
171+
constructor(data = []) {
172+
this.data = data;
173+
this.comparator = (a, b) => a - b;
174+
this.heapify();
175+
}
176+
177+
heapify() {
178+
if (this.size() < 2) return;
179+
for (let i = 1; i < this.size(); i++) {
180+
this.bubbleUp(i);
181+
}
182+
}
183+
184+
peek() {
185+
if (this.size() === 0) return null;
186+
return this.data[0];
187+
}
188+
189+
offer(value) {
190+
this.data.push(value);
191+
this.bubbleUp(this.size() - 1);
192+
}
193+
194+
poll() {
195+
if (this.size() === 0) {
196+
return null;
197+
}
198+
const result = this.data[0];
199+
const last = this.data.pop();
200+
if (this.size() !== 0) {
201+
this.data[0] = last;
202+
this.bubbleDown(0);
203+
}
204+
return result;
205+
}
206+
207+
bubbleUp(index) {
208+
while (index > 0) {
209+
const parentIndex = (index - 1) >> 1;
210+
if (this.comparator(this.data[index], this.data[parentIndex]) < 0) {
211+
this.swap(index, parentIndex);
212+
index = parentIndex;
213+
} else {
214+
break;
215+
}
216+
}
217+
}
218+
219+
bubbleDown(index) {
220+
const lastIndex = this.size() - 1;
221+
while (true) {
222+
const leftIndex = index * 2 + 1;
223+
const rightIndex = index * 2 + 2;
224+
let findIndex = index;
225+
if (
226+
leftIndex <= lastIndex &&
227+
this.comparator(this.data[leftIndex], this.data[findIndex]) < 0
228+
) {
229+
findIndex = leftIndex;
230+
}
231+
if (
232+
rightIndex <= lastIndex &&
233+
this.comparator(this.data[rightIndex], this.data[findIndex]) < 0
234+
) {
235+
findIndex = rightIndex;
236+
}
237+
if (index !== findIndex) {
238+
this.swap(index, findIndex);
239+
index = findIndex;
240+
} else {
241+
break;
242+
}
243+
}
244+
}
245+
246+
swap(index1, index2) {
247+
[this.data[index1], this.data[index2]] = [this.data[index2], this.data[index1]];
248+
}
249+
250+
size() {
251+
return this.data.length;
252+
}
253+
}
254+
255+
/**
256+
* Your KthLargest object will be instantiated and called as such:
257+
* var obj = new KthLargest(k, nums)
258+
* var param_1 = obj.add(val)
259+
*/
260+
```
261+
143262
### **Go**
144263

145264
```go

solution/0700-0799/0703.Kth Largest Element in a Stream/README_EN.md

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,125 @@ class KthLargest {
102102
*/
103103
```
104104

105+
### **TypeScript**
106+
107+
```js
108+
/**
109+
* @param {number} k
110+
* @param {number[]} nums
111+
*/
112+
var KthLargest = function(k, nums) {
113+
this.k = k;
114+
this.heap = new MinHeap();
115+
for (let num of nums) {
116+
this.add(num);
117+
}
118+
};
119+
120+
/**
121+
* @param {number} val
122+
* @return {number}
123+
*/
124+
KthLargest.prototype.add = function(val) {
125+
this.heap.offer(val);
126+
if (this.heap.size() > this.k) {
127+
this.heap.poll();
128+
}
129+
return this.heap.peek();
130+
};
131+
132+
class MinHeap {
133+
constructor(data = []) {
134+
this.data = data;
135+
this.comparator = (a, b) => a - b;
136+
this.heapify();
137+
}
138+
139+
heapify() {
140+
if (this.size() < 2) return;
141+
for (let i = 1; i < this.size(); i++) {
142+
this.bubbleUp(i);
143+
}
144+
}
145+
146+
peek() {
147+
if (this.size() === 0) return null;
148+
return this.data[0];
149+
}
150+
151+
offer(value) {
152+
this.data.push(value);
153+
this.bubbleUp(this.size() - 1);
154+
}
155+
156+
poll() {
157+
if (this.size() === 0) {
158+
return null;
159+
}
160+
const result = this.data[0];
161+
const last = this.data.pop();
162+
if (this.size() !== 0) {
163+
this.data[0] = last;
164+
this.bubbleDown(0);
165+
}
166+
return result;
167+
}
168+
169+
bubbleUp(index) {
170+
while (index > 0) {
171+
const parentIndex = (index - 1) >> 1;
172+
if (this.comparator(this.data[index], this.data[parentIndex]) < 0) {
173+
this.swap(index, parentIndex);
174+
index = parentIndex;
175+
} else {
176+
break;
177+
}
178+
}
179+
}
180+
181+
bubbleDown(index) {
182+
const lastIndex = this.size() - 1;
183+
while (true) {
184+
const leftIndex = index * 2 + 1;
185+
const rightIndex = index * 2 + 2;
186+
let findIndex = index;
187+
if (
188+
leftIndex <= lastIndex &&
189+
this.comparator(this.data[leftIndex], this.data[findIndex]) < 0
190+
) {
191+
findIndex = leftIndex;
192+
}
193+
if (
194+
rightIndex <= lastIndex &&
195+
this.comparator(this.data[rightIndex], this.data[findIndex]) < 0
196+
) {
197+
findIndex = rightIndex;
198+
}
199+
if (index !== findIndex) {
200+
this.swap(index, findIndex);
201+
index = findIndex;
202+
} else {
203+
break;
204+
}
205+
}
206+
}
207+
208+
swap(index1, index2) {
209+
[this.data[index1], this.data[index2]] = [this.data[index2], this.data[index1]];
210+
}
211+
212+
size() {
213+
return this.data.length;
214+
}
215+
}
216+
217+
/**
218+
* Your KthLargest object will be instantiated and called as such:
219+
* var obj = new KthLargest(k, nums)
220+
* var param_1 = obj.add(val)
221+
*/
222+
```
223+
105224
### **C++**
106225

107226
```cpp
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/**
2+
* @param {number} k
3+
* @param {number[]} nums
4+
*/
5+
var KthLargest = function(k, nums) {
6+
this.k = k;
7+
this.heap = new MinHeap();
8+
for (let num of nums) {
9+
this.add(num);
10+
}
11+
};
12+
13+
/**
14+
* @param {number} val
15+
* @return {number}
16+
*/
17+
KthLargest.prototype.add = function(val) {
18+
this.heap.offer(val);
19+
if (this.heap.size() > this.k) {
20+
this.heap.poll();
21+
}
22+
return this.heap.peek();
23+
};
24+
25+
class MinHeap {
26+
constructor(data = []) {
27+
this.data = data;
28+
this.comparator = (a, b) => a - b;
29+
this.heapify();
30+
}
31+
32+
heapify() {
33+
if (this.size() < 2) return;
34+
for (let i = 1; i < this.size(); i++) {
35+
this.bubbleUp(i);
36+
}
37+
}
38+
39+
peek() {
40+
if (this.size() === 0) return null;
41+
return this.data[0];
42+
}
43+
44+
offer(value) {
45+
this.data.push(value);
46+
this.bubbleUp(this.size() - 1);
47+
}
48+
49+
poll() {
50+
if (this.size() === 0) {
51+
return null;
52+
}
53+
const result = this.data[0];
54+
const last = this.data.pop();
55+
if (this.size() !== 0) {
56+
this.data[0] = last;
57+
this.bubbleDown(0);
58+
}
59+
return result;
60+
}
61+
62+
bubbleUp(index) {
63+
while (index > 0) {
64+
const parentIndex = (index - 1) >> 1;
65+
if (this.comparator(this.data[index], this.data[parentIndex]) < 0) {
66+
this.swap(index, parentIndex);
67+
index = parentIndex;
68+
} else {
69+
break;
70+
}
71+
}
72+
}
73+
74+
bubbleDown(index) {
75+
const lastIndex = this.size() - 1;
76+
while (true) {
77+
const leftIndex = index * 2 + 1;
78+
const rightIndex = index * 2 + 2;
79+
let findIndex = index;
80+
if (
81+
leftIndex <= lastIndex &&
82+
this.comparator(this.data[leftIndex], this.data[findIndex]) < 0
83+
) {
84+
findIndex = leftIndex;
85+
}
86+
if (
87+
rightIndex <= lastIndex &&
88+
this.comparator(this.data[rightIndex], this.data[findIndex]) < 0
89+
) {
90+
findIndex = rightIndex;
91+
}
92+
if (index !== findIndex) {
93+
this.swap(index, findIndex);
94+
index = findIndex;
95+
} else {
96+
break;
97+
}
98+
}
99+
}
100+
101+
swap(index1, index2) {
102+
[this.data[index1], this.data[index2]] = [this.data[index2], this.data[index1]];
103+
}
104+
105+
size() {
106+
return this.data.length;
107+
}
108+
}
109+
110+
/**
111+
* Your KthLargest object will be instantiated and called as such:
112+
* var obj = new KthLargest(k, nums)
113+
* var param_1 = obj.add(val)
114+
*/

0 commit comments

Comments
 (0)