< Summary

Information
Class: LeetCode.Algorithms.KthSmallestPrimeFraction.KthSmallestPrimeFractionBinarySearch
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\KthSmallestPrimeFraction\KthSmallestPrimeFractionBinarySearch.cs
Line coverage
100%
Covered lines: 65
Uncovered lines: 0
Coverable lines: 65
Total lines: 107
Line coverage: 100%
Branch coverage
95%
Covered branches: 23
Total branches: 24
Branch coverage: 95.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
KthSmallestPrimeFraction(...)95.83%2424100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\KthSmallestPrimeFraction\KthSmallestPrimeFractionBinarySearch.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.KthSmallestPrimeFraction;
 13
 14/// <inheritdoc />
 15public class KthSmallestPrimeFractionBinarySearch : IKthSmallestPrimeFraction
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n log n * log m)
 19    ///     Space complexity - O(k)
 20    /// </summary>
 21    /// <param name="arr"></param>
 22    /// <param name="k"></param>
 23    /// <returns></returns>
 24    public int[] KthSmallestPrimeFraction(int[] arr, int k)
 1025    {
 1026        var result = new int[2];
 27
 2028        double left = 0, right = 1.0;
 29
 2030        while (left < right)
 2031        {
 2032            var mid = (left + right) / 2;
 2033            var total = 0;
 2034            var p = 0;
 2035            var q = 1;
 2036            var j = 1;
 37
 2038            var heap = new SortedSet<(double value, int i, int j)>(Comparer<(double value, int i, int j)>.Create(
 2039                (a, b) =>
 4640                {
 4641                    var comparerResult = a.value.CompareTo(b.value);
 2042
 4643                    if (comparerResult != 0)
 4544                    {
 4545                        return comparerResult;
 2046                    }
 2047
 148                    comparerResult = a.i.CompareTo(b.i);
 2049
 150                    return comparerResult == 0 ? a.j.CompareTo(b.j) : comparerResult;
 6651                }));
 52
 14053            for (var i = 0; i < arr.Length - 1; i++)
 6354            {
 12555                while (j < arr.Length && arr[i] > mid * arr[j])
 6256                {
 6257                    j++;
 6258                }
 59
 6360                total += arr.Length - j;
 61
 6362                if (j == arr.Length)
 1363                {
 1364                    break;
 65                }
 66
 5067                if (arr[i] * q > arr[j] * p)
 3668                {
 3669                    p = arr[i];
 3670                    q = arr[j];
 3671                }
 72
 5073                heap.Add((arr[i] / (double)arr[j], i, j));
 74
 5075                if (heap.Count > k)
 176                {
 177                    heap.Remove(heap.Max);
 178                }
 5079            }
 80
 2081            if (total == k)
 1082            {
 1083                result[0] = p;
 1084                result[1] = q;
 85
 1086                break;
 87            }
 88
 1089            if (total < k)
 790            {
 791                left = mid;
 792            }
 93            else
 394            {
 395                right = mid;
 96
 397                if (heap.Count == k)
 198                {
 199                    result[0] = arr[heap.Max.i];
 1100                    result[1] = arr[heap.Max.j];
 1101                }
 3102            }
 10103        }
 104
 10105        return result;
 10106    }
 107}