3362. Zero Array Transformation III #1713
-
Topics: You are given an integer array Each
A Zero Array is an array with all its elements equal to 0. Return the maximum number of elements that can be removed from Example 1:
Example 2:
Example 3:
Example 4:
Example 5:
Constraints:
Hint:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
We need to determine the maximum number of queries that can be removed such that the remaining queries can still convert the given array into a zero array. Each query allows decrementing elements in a specified range by at most 1. The solution involves a greedy approach to select the most effective queries first. Approach
Let's implement this solution in PHP: 3362. Zero Array Transformation III <?php
class MaxHeap extends SplPriorityQueue {
/**
* @param $a
* @param $b
* @return int|mixed
*/
public function compare($a, $b) {
return $a - $b; // max-heap
}
}
class MinHeap extends SplPriorityQueue {
/**
* @param $a
* @param $b
* @return int|mixed
*/
public function compare($a, $b) {
return $b - $a; // min-heap
}
}
/**
* @param Integer[] $nums
* @param Integer[][] $queries
* @return Integer
*/
function maxRemoval($nums, $queries) {
$queryIndex = 0;
$available = new MaxHeap(); // max-heap for available `r`s
$running = new MinHeap(); // min-heap for running `r`s
// Sort queries by the first element
usort($queries, function($a, $b) {
return $a[0] - $b[0];
});
$n = count($nums);
for ($i = 0; $i < $n; ++$i) {
// Push to available all queries where l <= i
while ($queryIndex < count($queries) && $queries[$queryIndex][0] <= $i) {
$available->insert($queries[$queryIndex][1], $queries[$queryIndex][1]);
++$queryIndex;
}
// Remove all expired running tasks (r < i)
while (!$running->isEmpty() && $running->top() < $i) {
$running->extract();
}
// Add tasks from available until nums[i] <= count(running)
while ($nums[$i] > $running->count()) {
if ($available->isEmpty()) {
return -1;
}
// Get the max available r
$top = $available->top();
if ($top < $i) {
return -1;
}
$running->insert($top, $top);
$available->extract();
}
}
return $available->count();
}
// Example Test Cases
$nums1 = [2, 0, 2];
$queries1 = [[0, 2], [0, 2], [1, 1]];
echo maxRemoval($nums1, $queries1) . "\n"; // Output: 1
$nums2 = [1, 1, 1, 1];
$queries2 = [[1, 3], [0, 2], [1, 3], [1, 2]];
echo maxRemoval($nums2, $queries2) . "\n"; // Output: 2
$nums3 = [1, 2, 3, 4];
$queries3 = [[0, 3]];
echo maxRemoval($nums3, $queries3) . "\n"; // Output: -1
?> Explanation:
This approach ensures we use the minimum number of necessary queries, maximizing the number of queries that can be removed while still converting the array to zero. The complexity is efficient due to the use of heaps, making the solution scalable for large input sizes. |
Beta Was this translation helpful? Give feedback.
We need to determine the maximum number of queries that can be removed such that the remaining queries can still convert the given array into a zero array. Each query allows decrementing elements in a specified range by at most 1. The solution involves a greedy approach to select the most effective queries first.
Approach