< Summary

Information
Class: LeetCode.Algorithms.KthLargestElementInStream.KthLargestElementInStreamPriorityQueue
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\KthLargestElementInStream\KthLargestElementInStreamPriorityQueue.cs
Line coverage
100%
Covered lines: 21
Uncovered lines: 0
Coverable lines: 21
Total lines: 50
Line coverage: 100%
Branch coverage
100%
Covered branches: 6
Total branches: 6
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%22100%
Add(...)100%44100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\KthLargestElementInStream\KthLargestElementInStreamPriorityQueue.cs

#LineLine coverage
 1// --------------------------------------------------------------------------------
 2// Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv).
 3// All Rights Reserved.
 4// --------------------------------------------------------------------------------
 5// This software is the confidential and proprietary information of Eugene Eremeev
 6// (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not
 7// disclose such Confidential Information and shall use it only in accordance with
 8// the terms of the license agreement you entered into with Eugene Eremeev (also
 9// known as Yevhenii Yeriemeieiv).
 10// --------------------------------------------------------------------------------
 11
 12namespace LeetCode.Algorithms.KthLargestElementInStream;
 13
 14/// <inheritdoc />
 15public class KthLargestElementInStreamPriorityQueue : IKthLargestElementInStream
 16{
 17    private readonly int _k;
 218    private readonly PriorityQueue<int, int> _priorityQueue = new();
 19
 220    public KthLargestElementInStreamPriorityQueue(int k, int[] nums)
 221    {
 222        _k = k;
 23
 2624        foreach (var num in nums)
 1025        {
 1026            Add(num);
 1027        }
 228    }
 29
 30    /// <summary>
 31    ///     Time complexity - O(log k)
 32    ///     Space complexity - O(k)
 33    /// </summary>
 34    /// <param name="val"></param>
 35    /// <returns></returns>
 36    public int Add(int val)
 1937    {
 1938        if (_priorityQueue.Count < _k)
 739        {
 740            _priorityQueue.Enqueue(val, val);
 741        }
 1242        else if (val > _priorityQueue.Peek())
 743        {
 744            _priorityQueue.Dequeue();
 745            _priorityQueue.Enqueue(val, val);
 746        }
 47
 1948        return _priorityQueue.Peek();
 1949    }
 50}