< Summary

Information
Class: LeetCode.Algorithms.MostBeautifulItemForEachQuery.MostBeautifulItemForEachQueryBinarySearch
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MostBeautifulItemForEachQuery\MostBeautifulItemForEachQueryBinarySearch.cs
Line coverage
100%
Covered lines: 33
Uncovered lines: 0
Coverable lines: 33
Total lines: 69
Line coverage: 100%
Branch coverage
100%
Covered branches: 8
Total branches: 8
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
MaximumBeauty(...)100%88100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MostBeautifulItemForEachQuery\MostBeautifulItemForEachQueryBinarySearch.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.MostBeautifulItemForEachQuery;
 13
 14/// <inheritdoc />
 15public class MostBeautifulItemForEachQueryBinarySearch : IMostBeautifulItemForEachQuery
 16{
 17    /// <summary>
 18    ///     Time complexity - O((m + n) log m)
 19    ///     Space complexity - O(m + n)
 20    /// </summary>
 21    /// <param name="items"></param>
 22    /// <param name="queries"></param>
 23    /// <returns></returns>
 24    public int[] MaximumBeauty(int[][] items, int[] queries)
 325    {
 326        var result = new int[queries.Length];
 27
 1228        Array.Sort(items, (a, b) => a[0].CompareTo(b[0]));
 29
 330        var maxBeautyUpToPrice = new List<int[]>();
 331        var maxBeauty = 0;
 32
 2933        foreach (var item in items)
 1034        {
 1035            maxBeauty = Math.Max(maxBeauty, item[1]);
 1036            maxBeautyUpToPrice.Add([item[0], maxBeauty]);
 1037        }
 38
 4139        foreach (var queryWithIndex in queries.Select((query, index) => new[] { query, index }).OrderBy(q => q[0]))
 840        {
 841            var query = queryWithIndex[0];
 842            var originalIndex = queryWithIndex[1];
 43
 844            var low = 0;
 845            var high = maxBeautyUpToPrice.Count - 1;
 846            var beauty = 0;
 47
 3048            while (low <= high)
 2249            {
 2250                var mid = low + ((high - low) / 2);
 51
 2252                if (maxBeautyUpToPrice[mid][0] <= query)
 1653                {
 1654                    beauty = maxBeautyUpToPrice[mid][1];
 55
 1656                    low = mid + 1;
 1657                }
 58                else
 659                {
 660                    high = mid - 1;
 661                }
 2262            }
 63
 864            result[originalIndex] = beauty;
 865        }
 66
 367        return result;
 368    }
 69}