| | 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 KthSmallestPrimeFractionBruteForce : IKthSmallestPrimeFraction |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(n^2 log n) |
| | 19 | | /// Space complexity - O(n^2) |
| | 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 fractions = new List<(int i, int j, double fraction)>(); |
| | 27 | |
|
| 86 | 28 | | for (var i = 0; i < arr.Length - 1; i++) |
| 33 | 29 | | { |
| 228 | 30 | | for (var j = i + 1; j < arr.Length; j++) |
| 81 | 31 | | { |
| 81 | 32 | | fractions.Add((arr[i], arr[j], arr[i] / (double)arr[j])); |
| 81 | 33 | | } |
| 33 | 34 | | } |
| | 35 | |
|
| 89 | 36 | | var orderedFractions = fractions.OrderBy(f => f.fraction).ToArray(); |
| | 37 | |
|
| 10 | 38 | | return [orderedFractions[k - 1].i, orderedFractions[k - 1].j]; |
| 10 | 39 | | } |
| | 40 | | } |