< Summary

Information
Class: LeetCode.Algorithms.MinimumDeletionsForKMostKDistinctCharacters.MinimumDeletionsForKMostKDistinctCharactersFrequencyArrayPriorityQueue
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MinimumDeletionsForKMostKDistinctCharacters\MinimumDeletionsForKMostKDistinctCharactersFrequencyArrayPriorityQueue.cs
Line coverage
100%
Covered lines: 25
Uncovered lines: 0
Coverable lines: 25
Total lines: 60
Line coverage: 100%
Branch coverage
100%
Covered branches: 10
Total branches: 10
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
MinDeletion(...)100%1010100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MinimumDeletionsForKMostKDistinctCharacters\MinimumDeletionsForKMostKDistinctCharactersFrequencyArrayPriorityQueue.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.MinimumDeletionsForKMostKDistinctCharacters;
 13
 14/// <inheritdoc />
 15public class MinimumDeletionsForKMostKDistinctCharactersFrequencyArrayPriorityQueue :
 16    IMinimumDeletionsForKMostKDistinctCharacters
 17{
 18    /// <summary>
 19    ///     Time complexity - O(n)
 20    ///     Space complexity - O(1)
 21    /// </summary>
 22    /// <param name="s"></param>
 23    /// <param name="k"></param>
 24    /// <returns></returns>
 25    public int MinDeletion(string s, int k)
 426    {
 427        var frequencyArray = new int['z' - 'a' + 1];
 28
 4429        foreach (var c in s)
 1630        {
 1631            frequencyArray[c - 'a']++;
 1632        }
 33
 10834        var countToRemove = frequencyArray.Count(frequency => frequency > 0) - k;
 35
 436        if (countToRemove <= 0)
 137        {
 138            return 0;
 39        }
 40
 341        var priorityQueue = new PriorityQueue<int, int>();
 42
 16543        foreach (var frequency in frequencyArray)
 7844        {
 7845            if (frequency > 0)
 946            {
 947                priorityQueue.Enqueue(frequency, frequency);
 948            }
 7849        }
 50
 351        var result = 0;
 52
 1653        for (var i = 0; i < countToRemove; i++)
 554        {
 555            result += priorityQueue.Dequeue();
 556        }
 57
 358        return result;
 459    }
 60}