Categories

## LeetCode 703. Kth Largest Element in a Stream [heap] Java

Design a class to find the kth largest element in a stream. Note that it is the kth largest element in the sorted order, not the kth distinct element.

Your `KthLargest` class will have a constructor which accepts an integer `k` and an integer array `nums`, which contains initial elements from the stream. For each call to the method `KthLargest.add`, return the element representing the kth largest element in the stream.

Example:

```int k = 3;
int[] arr = [4,5,8,2];
KthLargest kthLargest = new KthLargest(3, arr);
```

Note:
You may assume that `nums`‘ length ≥ `k-1` and `k` ≥ 1.

Categories

## LeetCode 378. Kth Smallest Element in a Sorted Matrix [heap] [java]

Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix.

Note that it is the kth smallest element in the sorted order, not the kth distinct element.

Example:

```matrix = [
[ 1,  5,  9],
[10, 11, 13],
[12, 13, 15]
],
k = 8,

return 13.
```

Note:
You may assume k is always valid, 1 ≤ k ≤ n2.

Categories

## LeetCode 215. Kth Largest Element in an Array [Heap] Java

Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.

Example 1:

```Input: `[3,2,1,5,6,4] `and k = 2
Output: 5
```

Example 2:

```Input: `[3,2,3,1,2,4,5,5,6] `and k = 4
Output: 4```

Note:
You may assume k is always valid, 1 ≤ k ≤ array’s length.