给定一个无限的整数流,找出任意时间点的第 k 个最大元素。可以假设 1 <= k <= n。
Input:
stream[] = {10, 20, 11, 70, 50, 40, 100, 5, ...}
k = 3
Output: {_, _, 10, 11, 20, 40, 50, 50, ...}
允许的额外空间为 O(k)。
这个想法是使用最小堆。
1) 在最小堆中存储前 k 个元素。
2) 对于从 (k+1)-th 到第 n-th 的每个元素,请执行以下操作。
……a) 打印堆的根。
……b) 如果当前元素大于堆的根,则弹出根并插入
CPP
// CPP program to find k-th largest element in a
// stream after every insertion.
#include
using namespace std;
int kthLargest(int stream[], int n, int k)
{
// Create a min heap and store first k-1 elements
// of stream into
priority_queue, greater > pq;
// Push first k elements and print "_" (k-1) times
for (int i=0; i pq.top())
{
pq.pop();
pq.push(stream[i]);
}
}
// Print last k-th largest element (after
// (inserting last element)
cout << pq.top();
}
// Driver code
int main()
{
int arr[] = {10, 20, 11, 70, 50, 40, 100, 55};
int k = 3;
int n = sizeof(arr)/sizeof(arr[0]);
kthLargest(arr, n, k);
return 0;
}
Java
// Java Program for the above approach
import java.util.*;
class GFG
{
static void kthLargest(int stream[], int n, int k)
{
// Create a min heap and store first k-1 elements
// of stream into
Vector pq = new Vector(n);
// Push first k elements and print "_" (k-1) times
for (int i = 0; i < k - 1; i++)
{
pq.add(stream[i]);
System.out.print("_ ");
}
pq.add(stream[k - 1]);
for (int i = k; i < n; i++)
{
// We must insert last element before we
// decide last k-th largest output.
Collections.sort(pq);
System.out.print(pq.get(0) + " ");
if (stream[i] > pq.get(0))
{
pq.remove(0);
pq.add(stream[i]);
}
}
// Print last k-th largest element (after
// (inserting last element)
Collections.sort(pq);
System.out.print(pq.get(0));
}
// Driver code
public static void main(String[] args) {
int arr[] = {10, 20, 11, 70, 50, 40, 100, 55};
int k = 3;
int n = arr.length;
kthLargest(arr, n, k);
}
}
// This code is contributed by divyeshrabadiya07.
Python3
# Python Program for the above approach
def kthLargest(stream, n, k):
# Create a min heap and store first k-1 elements
# of stream into
pq = []
# Push first k elements and print "_" (k-1) times
for i in range(k - 1):
pq.append(stream[i])
print("_ ", end = "")
pq.append(stream[k - 1])
for i in range(k, n):
# We must insert last element before we
# decide last k-th largest output.
pq.sort()
print(pq[0], end = " ")
if(stream[i] > pq[0]):
del pq[0]
pq.append(stream[i])
# Print last k-th largest element (after
# (inserting last element)
pq.sort()
print(pq[0], end = "")
# Driver code
arr = [10, 20, 11, 70, 50, 40, 100, 55]
k = 3
n = len(arr)
kthLargest(arr, n, k)
# This code is contributed by avanitrachhadiya2155
C#
// C# program to find k-th largest element in a
// stream after every insertion.
using System;
using System.Collections.Generic;
class GFG
{
static void kthLargest(int[] stream, int n, int k)
{
// Create a min heap and store first k-1 elements
// of stream into
List pq = new List();
// Push first k elements and print "_" (k-1) times
for (int i = 0; i < k - 1; i++)
{
pq.Add(stream[i]);
Console.Write("_ ");
}
pq.Add(stream[k - 1]);
for (int i = k; i < n; i++)
{
// We must insert last element before we
// decide last k-th largest output.
pq.Sort();
Console.Write(pq[0] + " ");
if (stream[i] > pq[0])
{
pq.RemoveAt(0);
pq.Add(stream[i]);
}
}
// Print last k-th largest element (after
// (inserting last element)
pq.Sort();
Console.Write(pq[0]);
}
// Driver code
static void Main()
{
int[] arr = {10, 20, 11, 70, 50, 40, 100, 55};
int k = 3;
int n = arr.Length;
kthLargest(arr, n, k);
}
}
// This code is contributed by divyesh072019.
Javascript
输出:
_ _ 10 11 20 40 50 55
如果流包含非原始类型的元素,我们可以定义我们自己的压缩器函数并相应地创建一个 priority_queue。
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。