Skip to content

Commit c74d816

Browse files
committed
feat: add ts solution to lc problem: No.0215
1 parent 46419ab commit c74d816

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

solution/0200-0299/0215.Kth Largest Element in an Array/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,4 +309,30 @@ impl Solution {
309309

310310
<!-- solution:end -->
311311

312+
<!-- solution:start -->
313+
314+
### Solution 3: Max priority heap
315+
316+
<!-- tabs:start -->
317+
318+
#### TypeScript
319+
320+
```ts
321+
function findKthLargest(nums: number[], k: number): number {
322+
const maxPQ = new MaxPriorityQueue();
323+
for (const x of nums) {
324+
maxPQ.enqueue(x);
325+
}
326+
327+
let res = 0;
328+
while (k--) res = maxPQ.dequeue().element;
329+
330+
return res;
331+
}
332+
```
333+
334+
<!-- tabs:end -->
335+
336+
<!-- solution:end -->
337+
312338
<!-- problem:end -->

solution/0200-0299/0215.Kth Largest Element in an Array/README_EN.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,4 +301,30 @@ impl Solution {
301301

302302
<!-- solution:end -->
303303

304+
<!-- solution:start -->
305+
306+
### Solution 3: Max priority heap
307+
308+
<!-- tabs:start -->
309+
310+
#### TypeScript
311+
312+
```ts
313+
function findKthLargest(nums: number[], k: number): number {
314+
const maxPQ = new MaxPriorityQueue();
315+
for (const x of nums) {
316+
maxPQ.enqueue(x);
317+
}
318+
319+
let res = 0;
320+
while (k--) res = maxPQ.dequeue().element;
321+
322+
return res;
323+
}
324+
```
325+
326+
<!-- tabs:end -->
327+
328+
<!-- solution:end -->
329+
304330
<!-- problem:end -->
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function findKthLargest(nums: number[], k: number): number {
2+
const maxPQ = new MaxPriorityQueue();
3+
for (const x of nums) {
4+
maxPQ.enqueue(x);
5+
}
6+
7+
let res = 0;
8+
while (k--) res = maxPQ.dequeue().element;
9+
10+
return res;
11+
}

0 commit comments

Comments
 (0)