< Summary

Information
Class: LeetCode.Algorithms.KthLargestElementInStream.KthLargestElementInStreamSortedArray
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\KthLargestElementInStream\KthLargestElementInStreamSortedArray.cs
Line coverage
100%
Covered lines: 27
Uncovered lines: 0
Coverable lines: 27
Total lines: 63
Line coverage: 100%
Branch coverage
90%
Covered branches: 9
Total branches: 10
Branch coverage: 90%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

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

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\KthLargestElementInStream\KthLargestElementInStreamSortedArray.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 KthLargestElementInStreamSortedArray : IKthLargestElementInStream
 16{
 17    private readonly int _k;
 18    private readonly int[] _nums;
 19
 220    public KthLargestElementInStreamSortedArray(int k, int[] nums)
 221    {
 222        _k = k;
 23
 224        var newNums = new int[k];
 25
 226        Array.Fill(newNums, int.MinValue);
 27
 228        if (nums.Length > 0)
 229        {
 1430            Array.Sort(nums, (a, b) => b.CompareTo(a));
 31
 232            var i = 0;
 33
 934            while (i < nums.Length && i < k)
 735            {
 736                newNums[i] = nums[i];
 37
 738                i++;
 739            }
 240        }
 41
 242        _nums = newNums;
 243    }
 44
 45    /// <summary>
 46    ///     Time complexity - O(k)
 47    ///     Space complexity - O(k)
 48    /// </summary>
 49    /// <param name="val"></param>
 50    /// <returns></returns>
 51    public int Add(int val)
 952    {
 8053        for (var i = 0; i < _nums.Length; i++)
 3154        {
 3155            if (_nums[i] < val)
 1156            {
 1157                (_nums[i], val) = (val, _nums[i]);
 1158            }
 3159        }
 60
 961        return _nums[_k - 1];
 962    }
 63}