-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
3066. Minimum Operations to Exceed Threshold Value II #1310
Comments
We need to determine the minimum number of operations required to ensure all elements in a given array are greater than or equal to a specified threshold value Approach
Let's implement this solution in PHP: 3066. Minimum Operations to Exceed Threshold Value II <?php
/**
* @param Integer[] $nums
* @param Integer $k
* @return Integer
*/
function minOperations($nums, $k) {
$heap = new SplMinHeap();
foreach ($nums as $num) {
$heap->insert($num);
}
$count = 0;
while ($heap->count() >= 2) {
if ($heap->top() >= $k) {
break;
}
$x = $heap->extract();
$y = $heap->extract();
$new_val = 2 * $x + $y;
$heap->insert($new_val);
$count++;
}
return $count;
}
// Example Test Cases
$nums1 = [2, 11, 10, 1, 3];
$k1 = 10;
echo minOperations($nums1, $k1) . "\n"; // Output: 2
$nums2 = [1, 1, 2, 4, 9];
$k2 = 20;
echo minOperations($nums2, $k2) . "\n"; // Output: 4
?> Explanation:
This approach ensures that we minimize the number of operations by always combining the smallest elements first, which helps in quickly increasing the values to meet the threshold. The use of a min-heap allows efficient retrieval and insertion operations, making the solution optimal for large input sizes. |
…reshold-value-ii submissions 1541852421 Co-authored-by: kovatz <[email protected]> Co-authored-by: topugit <[email protected]> Co-authored-by: basharul-siddike <[email protected]> Co-authored-by: hafijul233 <[email protected]>
Discussed in #1309
Originally posted by mah-shamim February 13, 2025
Topics:
Array
,Heap (Priority Queue)
,Simulation
You are given a 0-indexed integer array
nums
, and an integerk
.In one operation, you will:
x
andy
innums
.x
andy
fromnums
.min(x, y) * 2 + max(x, y)
anywhere in the array.Note that you can only apply the described operation if
nums
contains at least two elements.Return the minimum number of operations needed so that all elements of the array are greater than or equal to
k
.Example 1:
In the second operation, we remove elements 3 and 4, then add 3 * 2 + 4 to nums. nums becomes equal to [10, 11, 10].
At this stage, all the elements of nums are greater than or equal to 10 so we can stop.
It can be shown that 2 is the minimum number of operations needed so that all elements of the array are greater than or equal to 10.
Example 2:
After two operations, nums becomes equal to [7, 4, 9].
After three operations, nums becomes equal to [15, 9].
After four operations, nums becomes equal to [33].
At this stage, all the elements of nums are greater than 20 so we can stop.
It can be shown that 4 is the minimum number of operations needed so that all elements of the array are greater than or equal to 20.
Constraints:
2 <= nums.length <= 2 * 105
1 <= nums[i] <= 109
1 <= k <= 109
k
.Hint:
The text was updated successfully, but these errors were encountered: