Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit db83d05

Browse files
authored
feat: add ts solution to lc problem: No.0703 (doocs#3186)
1 parent 91e3052 commit db83d05

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,39 @@ class MinHeap {
349349
*/
350350
```
351351

352+
#### TypeScript
353+
354+
```ts
355+
class KthLargest {
356+
#pq = new MinPriorityQueue();
357+
#k = 0;
358+
359+
constructor(k: number, nums: number[]) {
360+
this.#k = k;
361+
for (const x of nums) {
362+
this.#pq.enqueue(x);
363+
if (this.#pq.size() > k) {
364+
this.#pq.dequeue();
365+
}
366+
}
367+
}
368+
369+
add(val: number): number {
370+
this.#pq.enqueue(val);
371+
if (this.#pq.size() > this.#k) {
372+
this.#pq.dequeue();
373+
}
374+
return this.#pq.front().element;
375+
}
376+
}
377+
378+
/**
379+
* Your KthLargest object will be instantiated and called as such:
380+
* var obj = new KthLargest(k, nums)
381+
* var param_1 = obj.add(val)
382+
*/
383+
```
384+
352385
<!-- tabs:end -->
353386

354387
<!-- solution:end -->

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,39 @@ class MinHeap {
348348
*/
349349
```
350350

351+
#### TypeScript
352+
353+
```ts
354+
class KthLargest {
355+
#pq = new MinPriorityQueue();
356+
#k = 0;
357+
358+
constructor(k: number, nums: number[]) {
359+
this.#k = k;
360+
for (const x of nums) {
361+
this.#pq.enqueue(x);
362+
if (this.#pq.size() > k) {
363+
this.#pq.dequeue();
364+
}
365+
}
366+
}
367+
368+
add(val: number): number {
369+
this.#pq.enqueue(val);
370+
if (this.#pq.size() > this.#k) {
371+
this.#pq.dequeue();
372+
}
373+
return this.#pq.front().element;
374+
}
375+
}
376+
377+
/**
378+
* Your KthLargest object will be instantiated and called as such:
379+
* var obj = new KthLargest(k, nums)
380+
* var param_1 = obj.add(val)
381+
*/
382+
```
383+
351384
<!-- tabs:end -->
352385

353386
<!-- solution:end -->
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class KthLargest {
2+
#pq = new MinPriorityQueue();
3+
#k = 0;
4+
5+
constructor(k: number, nums: number[]) {
6+
this.#k = k;
7+
for (const x of nums) {
8+
this.#pq.enqueue(x);
9+
if (this.#pq.size() > k) {
10+
this.#pq.dequeue();
11+
}
12+
}
13+
}
14+
15+
add(val: number): number {
16+
this.#pq.enqueue(val);
17+
if (this.#pq.size() > this.#k) {
18+
this.#pq.dequeue();
19+
}
20+
return this.#pq.front().element;
21+
}
22+
}
23+
24+
/**
25+
* Your KthLargest object will be instantiated and called as such:
26+
* var obj = new KthLargest(k, nums)
27+
* var param_1 = obj.add(val)
28+
*/

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /