| | 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 | |
|
| | 12 | | namespace LeetCode.Algorithms.KthSmallestPrimeFraction; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public 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) |
| 10 | 25 | | { |
| 10 | 26 | | var result = new int[2]; |
| | 27 | |
|
| 20 | 28 | | double left = 0, right = 1.0; |
| | 29 | |
|
| 20 | 30 | | while (left < right) |
| 20 | 31 | | { |
| 20 | 32 | | var mid = (left + right) / 2; |
| 20 | 33 | | var total = 0; |
| 20 | 34 | | var p = 0; |
| 20 | 35 | | var q = 1; |
| 20 | 36 | | var j = 1; |
| | 37 | |
|
| 20 | 38 | | var heap = new SortedSet<(double value, int i, int j)>(Comparer<(double value, int i, int j)>.Create( |
| 20 | 39 | | (a, b) => |
| 46 | 40 | | { |
| 46 | 41 | | var comparerResult = a.value.CompareTo(b.value); |
| 20 | 42 | |
|
| 46 | 43 | | if (comparerResult != 0) |
| 45 | 44 | | { |
| 45 | 45 | | return comparerResult; |
| 20 | 46 | | } |
| 20 | 47 | |
|
| 1 | 48 | | comparerResult = a.i.CompareTo(b.i); |
| 20 | 49 | |
|
| 1 | 50 | | return comparerResult == 0 ? a.j.CompareTo(b.j) : comparerResult; |
| 66 | 51 | | })); |
| | 52 | |
|
| 140 | 53 | | for (var i = 0; i < arr.Length - 1; i++) |
| 63 | 54 | | { |
| 125 | 55 | | while (j < arr.Length && arr[i] > mid * arr[j]) |
| 62 | 56 | | { |
| 62 | 57 | | j++; |
| 62 | 58 | | } |
| | 59 | |
|
| 63 | 60 | | total += arr.Length - j; |
| | 61 | |
|
| 63 | 62 | | if (j == arr.Length) |
| 13 | 63 | | { |
| 13 | 64 | | break; |
| | 65 | | } |
| | 66 | |
|
| 50 | 67 | | if (arr[i] * q > arr[j] * p) |
| 36 | 68 | | { |
| 36 | 69 | | p = arr[i]; |
| 36 | 70 | | q = arr[j]; |
| 36 | 71 | | } |
| | 72 | |
|
| 50 | 73 | | heap.Add((arr[i] / (double)arr[j], i, j)); |
| | 74 | |
|
| 50 | 75 | | if (heap.Count > k) |
| 1 | 76 | | { |
| 1 | 77 | | heap.Remove(heap.Max); |
| 1 | 78 | | } |
| 50 | 79 | | } |
| | 80 | |
|
| 20 | 81 | | if (total == k) |
| 10 | 82 | | { |
| 10 | 83 | | result[0] = p; |
| 10 | 84 | | result[1] = q; |
| | 85 | |
|
| 10 | 86 | | break; |
| | 87 | | } |
| | 88 | |
|
| 10 | 89 | | if (total < k) |
| 7 | 90 | | { |
| 7 | 91 | | left = mid; |
| 7 | 92 | | } |
| | 93 | | else |
| 3 | 94 | | { |
| 3 | 95 | | right = mid; |
| | 96 | |
|
| 3 | 97 | | if (heap.Count == k) |
| 1 | 98 | | { |
| 1 | 99 | | result[0] = arr[heap.Max.i]; |
| 1 | 100 | | result[1] = arr[heap.Max.j]; |
| 1 | 101 | | } |
| 3 | 102 | | } |
| 10 | 103 | | } |
| | 104 | |
|
| 10 | 105 | | return result; |
| 10 | 106 | | } |
| | 107 | | } |