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.

Categories

## Heap and Heap-Sort algorithm (Java code and Leetcode problems)

Note: Heap in algorithm and data structure is a different concept with the heap in memory management. Heap in memory management is more like a pool. There is a discussion at StackOverflow, we don’t talk about the detail of this different. You know we are talking about the heap concept in algorithm and data structure is enough for us. We still follow the style of Introduction to Algorithms.

Categories

## LeetCode problem 23 Merge k Sorted Lists [Divide and conquer][Min-Heap]（Java）

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.

Example:

Input:
[
1->4->5,
1->3->4,
2->6
]
Output:
1->1->2->3->4->4->5->6