Sponsored
This approach works by iterating from the end of the array to the beginning. The idea is to ensure each element is not bigger than the rest of the previously sorted part of the array. This can be optimized using a priority queue to store the required values efficiently, making it easier to decide where to split large numbers.
Whenever we find a number that is greater than the required maximum, we split it and keep track of splits using the priority queue (min-heap in this case).
Time Complexity: O(N log N), primarily due to the heap operations.
Space Complexity: O(N) due to the additional space used by the priority queue.
1import heapq
2
3def min_replacements(nums):
4 pq = []
5 operations = 0
6 for num in reversed(nums):
7 if pq and num > pq[0]:
8 operations += 1
9 half = num // 2
10 heapq.heappush(pq, half)
11 heapq.heappush(pq, half + num % 2)
12 else:
13 heapq.heappush(pq, num)
14 return operations
15
16# Example usage
17print(min_replacements([3, 9, 3])) # Output: 2
18print(min_replacements([1, 2, 3, 4, 5])) # Output: 0
The solution uses a priority queue to manage elements efficiently. By iterating the array from end to start, we can ensure that each number is processed in a priority-wise manner, allowing us to minimize operations by only splitting when necessary.
This approach relies on a greedy approach combined with a two-pointer technique. The algorithm will scan the array while managing smaller replacements for larger numbers. The idea is to minimize the change needed by checking previous and current indices simultaneously to determine necessary operations.
Time Complexity: O(N^2) with sorting for maintaining order.
Space Complexity: O(N) due to the auxiliary vector space used.
1#include <vector>
2#include <algorithm>
3
4int minReplacements(std::vector<int>& nums) {
5 int operations = 0;
int n = nums.size();
std::vector<int> small;
for (int i = n - 1; i >= 0; --i) {
if (!small.empty() && nums[i] > small.back()) {
operations++;
int half = nums[i] / 2;
small.push_back(half);
small.push_back(half + nums[i] % 2);
std::sort(small.begin(), small.end());
} else {
small.push_back(nums[i]);
}
}
return operations;
}
// Example usage
#include <iostream>
int main() {
std::vector<int> nums1 = {3, 9, 3};
std::cout << minReplacements(nums1) << std::endl; // Output: 2
std::vector<int> nums2 = {1, 2, 3, 4, 5};
std::cout << minReplacements(nums2) << std::endl; // Output: 0
return 0;
}
The C++ solution uses a vector to maintain the smaller elements found during the traversal from end to start of the array, making controlled adjustments to ensure non-decreasing order.
We maintain order using sorting and check conditions dynamically while iterating.