| | | 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.MinimumCostToHireKWorkers; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// https://leetcode.com/problems/minimum-cost-to-hire-k-workers/ |
| | | 16 | | /// </summary> |
| | | 17 | | public sealed class MinimumCostToHireKWorkersPriorityQueue : IMinimumCostToHireKWorkers |
| | | 18 | | { |
| | | 19 | | /// <summary> |
| | | 20 | | /// Time complexity - O(n log n) |
| | | 21 | | /// Space complexity - O(n) |
| | | 22 | | /// </summary> |
| | | 23 | | /// <param name="quality"></param> |
| | | 24 | | /// <param name="wage"></param> |
| | | 25 | | /// <param name="k"></param> |
| | | 26 | | /// <returns></returns> |
| | | 27 | | public double MincostToHireWorkers(int[] quality, int[] wage, int k) |
| | 5 | 28 | | { |
| | 5 | 29 | | var workers = new (double ratio, int quality)[quality.Length]; |
| | | 30 | | |
| | 68 | 31 | | for (var i = 0; i < quality.Length; ++i) |
| | 29 | 32 | | { |
| | 29 | 33 | | workers[i] = ((double)wage[i] / quality[i], quality[i]); |
| | 29 | 34 | | } |
| | | 35 | | |
| | 68 | 36 | | Array.Sort(workers, (a, b) => a.ratio.CompareTo(b.ratio)); |
| | | 37 | | |
| | 5 | 38 | | var result = double.MaxValue; |
| | | 39 | | |
| | 5 | 40 | | var priorityQueue = new PriorityQueue<double, double>(); |
| | | 41 | | |
| | 5 | 42 | | double sum = 0; |
| | | 43 | | |
| | 73 | 44 | | foreach (var worker in workers) |
| | 29 | 45 | | { |
| | 29 | 46 | | sum += worker.quality; |
| | | 47 | | |
| | 29 | 48 | | priorityQueue.Enqueue(-worker.quality, -worker.quality); |
| | | 49 | | |
| | 29 | 50 | | if (priorityQueue.Count > k) |
| | 17 | 51 | | { |
| | 17 | 52 | | sum += priorityQueue.Dequeue(); |
| | 17 | 53 | | } |
| | | 54 | | |
| | 29 | 55 | | if (priorityQueue.Count == k) |
| | 22 | 56 | | { |
| | 22 | 57 | | result = Math.Min(result, sum * worker.ratio); |
| | 22 | 58 | | } |
| | 29 | 59 | | } |
| | | 60 | | |
| | 5 | 61 | | return result; |
| | 5 | 62 | | } |
| | | 63 | | } |