| | | 1 | | // -------------------------------------------------------------------------------- |
| | | 2 | | // Copyright (C) 2026 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 | | |
| | | 12 | | namespace LeetCode.Algorithms.MostBeautifulItemForEachQuery; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public sealed 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) |
| | 3 | 25 | | { |
| | 3 | 26 | | var result = new int[queries.Length]; |
| | | 27 | | |
| | 12 | 28 | | Array.Sort(items, (a, b) => a[0].CompareTo(b[0])); |
| | | 29 | | |
| | 3 | 30 | | var maxBeautyUpToPrice = new List<int[]>(); |
| | 3 | 31 | | var maxBeauty = 0; |
| | | 32 | | |
| | 29 | 33 | | foreach (var item in items) |
| | 10 | 34 | | { |
| | 10 | 35 | | maxBeauty = Math.Max(maxBeauty, item[1]); |
| | 10 | 36 | | maxBeautyUpToPrice.Add([item[0], maxBeauty]); |
| | 10 | 37 | | } |
| | | 38 | | |
| | 41 | 39 | | foreach (var queryWithIndex in queries.Select((query, index) => new[] { query, index }).OrderBy(q => q[0])) |
| | 8 | 40 | | { |
| | 8 | 41 | | var query = queryWithIndex[0]; |
| | 8 | 42 | | var originalIndex = queryWithIndex[1]; |
| | | 43 | | |
| | 8 | 44 | | var low = 0; |
| | 8 | 45 | | var high = maxBeautyUpToPrice.Count - 1; |
| | 8 | 46 | | var beauty = 0; |
| | | 47 | | |
| | 30 | 48 | | while (low <= high) |
| | 22 | 49 | | { |
| | 22 | 50 | | var mid = low + ((high - low) / 2); |
| | | 51 | | |
| | 22 | 52 | | if (maxBeautyUpToPrice[mid][0] <= query) |
| | 16 | 53 | | { |
| | 16 | 54 | | beauty = maxBeautyUpToPrice[mid][1]; |
| | | 55 | | |
| | 16 | 56 | | low = mid + 1; |
| | 16 | 57 | | } |
| | | 58 | | else |
| | 6 | 59 | | { |
| | 6 | 60 | | high = mid - 1; |
| | 6 | 61 | | } |
| | 22 | 62 | | } |
| | | 63 | | |
| | 8 | 64 | | result[originalIndex] = beauty; |
| | 8 | 65 | | } |
| | | 66 | | |
| | 3 | 67 | | return result; |
| | 3 | 68 | | } |
| | | 69 | | } |