| | 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.MinimumOperationsToExceedThresholdValue2; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class MinimumOperationsToExceedThresholdValue2PriorityQueue : IMinimumOperationsToExceedThresholdValue2 |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(n log n) |
| | 19 | | /// Space complexity - O(n) |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="nums"></param> |
| | 22 | | /// <param name="k"></param> |
| | 23 | | /// <returns></returns> |
| | 24 | | public int MinOperations(int[] nums, int k) |
| 2 | 25 | | { |
| 2 | 26 | | var minOperations = 0; |
| | 27 | |
|
| 2 | 28 | | var priorityQueue = new PriorityQueue<long, long>(); |
| | 29 | |
|
| 26 | 30 | | foreach (var num in nums) |
| 10 | 31 | | { |
| 10 | 32 | | priorityQueue.Enqueue(num, num); |
| 10 | 33 | | } |
| | 34 | |
|
| 8 | 35 | | while (priorityQueue.Count > 1 && priorityQueue.Peek() < k) |
| 6 | 36 | | { |
| 6 | 37 | | var first = priorityQueue.Dequeue(); |
| 6 | 38 | | var second = priorityQueue.Dequeue(); |
| | 39 | |
|
| 6 | 40 | | var num = (first * 2) + second; |
| | 41 | |
|
| 6 | 42 | | priorityQueue.Enqueue(num, num); |
| | 43 | |
|
| 6 | 44 | | minOperations++; |
| 6 | 45 | | } |
| | 46 | |
|
| 2 | 47 | | return minOperations; |
| 2 | 48 | | } |
| | 49 | | } |