Leetcode 2869. Minimum Operations to Collect Elements
You are given an array of integers, nums, and another array, elements. We would like to remove the minimum number of elements from nums such that all of the elements in elements are no longer in nums. If it’s not possible, return -1.
In other words, you want to find the smallest subset of nums such that removing it will exclude all the integers in elements from nums.
elements guaranteed to be present in nums?
elements is not in nums would be necessary.nums can contain duplicate elements, and we have to consider them accordingly.n be the length of nums, and m be the length of elements. We’ll assume minimal constraints where arrays can be reasonably large given typical problem constraints (e.g., 1 ≤ n, m ≤ 100,000).The strategy can be broken down into the following steps:
elements to know how many times each element needs to be removed from nums.nums that contains all elements from elements with at least their required frequencies.Here is the C++ implementation following the described strategy:
#include <vector>
#include <unordered_map>
#include <iostream>
int minOperationsToCollectElementsOut(std::vector<int>& nums, std::vector<int>& elements) {
std::unordered_map<int, int> elementCount, windowCount;
// Count the frequency of elements in the `elements` array
for (const auto& ele : elements) {
elementCount[ele]++;
}
int required = elementCount.size(); // Number of unique elements we need to match
int formed = 0; // Number of unique elements we have successfully matched
int left = 0, right = 0;
int minLen = nums.size() + 1;
while (right < nums.size()) {
int rtEle = nums[right];
windowCount[rtEle]++;
if (elementCount.count(rtEle) && windowCount[rtEle] == elementCount[rtEle]) {
formed++;
}
while (left <= right && formed == required) {
int ltEle = nums[left];
if (right - left + 1 < minLen) {
minLen = right - left + 1;
}
windowCount[ltEle]--;
if (elementCount.count(ltEle) && windowCount[ltEle] < elementCount[ltEle]) {
formed--;
}
left++;
}
right++;
}
return minLen == nums.size() + 1 ? -1 : minLen;
}
int main() {
std::vector<int> nums = {1, 3, 5, 3, 1, 3, 7, 3};
std::vector<int> elements = {1, 3};
std::cout << "Minimum operations: " << minOperationsToCollectElementsOut(nums, elements) << std::endl;
return 0;
}
elements.nums.Hence, the overall time complexity is O(n + m).
This approach ensures that we are efficiently finding the smallest subarray in nums that contains all elements with the required frequency from elements.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?