< Summary

Information
Class: LeetCode.Algorithms.MaximumAveragePassRatio.MaximumAveragePassRatioPriorityQueue
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MaximumAveragePassRatio\MaximumAveragePassRatioPriorityQueue.cs
Line coverage
100%
Covered lines: 30
Uncovered lines: 0
Coverable lines: 30
Total lines: 69
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
MaxAverageRatio(...)100%66100%
GetDelta(...)100%11100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MaximumAveragePassRatio\MaximumAveragePassRatioPriorityQueue.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.MaximumAveragePassRatio;
 13
 14/// <inheritdoc />
 15public class MaximumAveragePassRatioPriorityQueue : IMaximumAveragePassRatio
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n log n)
 19    ///     Space complexity - O(n)
 20    /// </summary>
 21    /// <param name="classes"></param>
 22    /// <param name="extraStudents"></param>
 23    /// <returns></returns>
 24    public double MaxAverageRatio(int[][] classes, int extraStudents)
 225    {
 226        var ratiosPriorityQueue = new PriorityQueue<(int Pass, int Total), double>();
 27
 2028        foreach (var @class in classes)
 729        {
 730            var pass = @class[0];
 731            var total = @class[1];
 32
 733            var delta = GetDelta(pass, total);
 34
 735            ratiosPriorityQueue.Enqueue((pass, total), -delta);
 736        }
 37
 1638        for (var i = 0; i < extraStudents; i++)
 639        {
 640            var ratio = ratiosPriorityQueue.Dequeue();
 41
 642            ratio.Pass++;
 643            ratio.Total++;
 44
 645            var delta = GetDelta(ratio.Pass, ratio.Total);
 46
 647            ratiosPriorityQueue.Enqueue((ratio.Pass, ratio.Total), -delta);
 648        }
 49
 250        double average = 0;
 51
 952        while (ratiosPriorityQueue.Count > 0)
 753        {
 754            var ratio = ratiosPriorityQueue.Dequeue();
 55
 756            average += (double)ratio.Pass / ratio.Total / classes.Length;
 757        }
 58
 259        return average;
 260    }
 61
 62    private static double GetDelta(int pass, int total)
 1363    {
 1364        var currentRatio = (double)pass / total;
 1365        var newRatio = (double)(pass + 1) / (total + 1);
 66
 1367        return newRatio - currentRatio;
 1368    }
 69}