413. Find Kth Largest Using a Linked List Min-Heap

HardLinked ListHeapLinked ListSelection

Given a linked list of integers and an integer k, return the k-th largest value (by sorted order, counting duplicates as distinct positions). Maintain a min-heap of size k while scanning the list. If k is larger than the number of nodes, return -1. Input: '[list], k'.

Input: '[list], k'.

Output: Integer — the k-th largest value, or -1.

Examples

Example 1
Input: [3,2,1,5,6,4], 2
Output: 5
Explanation: 2nd largest is 5.
Example 2
Input: [3,2,3,1,2,4,5,5,6], 4
Output: 4
Explanation: 4th largest counting duplicates.
Example 3
Input: [1], 1
Output: 1
Explanation: Only element.

Constraints

Asked by

AmazonGoogleMicrosoftMetaAdobe
Solve this problem in the editor →