Home > Article > Backend Development > Minimize the number of unequal elements at corresponding indices between the given arrays
Compare the elements at each index and adjust them until they match to reduce the number of inconsistent elements at corresponding indices between the given arrays. Adjust as necessary while simultaneously iterating through the arrays. The arrays will become more similar and the proportion of unequal elements will decrease as a result. By reducing their differences at corresponding positions, this process seeks to increase the similarity between the arrays. The ultimate objective is to produce arrays with the same elements at every index, which will decrease the number of unequal elements.
Hashing Approach
Sorting Approach
Within the Hashing Approach, we begin by making a hash table for one of the arrays in order to diminish the number of unequal components when comparing files between the arrays. At that point, as we repeat through the moment array, we look at the frequency of each component within the hash table. In the event that the component is found, it is kept; in the event that it is not, the closest coordinating component from the hash table is utilised in its place. As a result of this process, there are fewer unequal elements at corresponding indices and both arrays become more similar. This method's efficiency is an advantage because it can achieve the desired similarity in linear time complexity O(N) for average and best cases.
Each element of the first array should be added as keys and their frequencies as values to a hash table.
Set up a pointer so you can cycle through the second array.
a. Determine whether each element in the second array is present in the hash table.
b. If so, leave the element alone.
如果没有的话,找到最接近的匹配项中频率最低的哈希表元素。
d. Change the existing element in the second array to the closest match.
直到指针达到第二个数组的末尾,重复步骤3再次执行
由于数组的存在,相应索引处的不相等元素数量现在将达到最低水平
The desired similarity to the first array is present in the modified second array.
#include <iostream> #include <unordered_map> #include <vector> #include <climits> using namespace std; void adjustArray(vector<int>& arr1, vector<int>& arr2) { unordered_map<int, int> frequency; for (int num : arr1) { frequency[num]++; } int ptr = 0; while (ptr < arr2.size()) { if (frequency.find(arr2[ptr]) != frequency.end()) { frequency[arr2[ptr]]--; if (frequency[arr2[ptr]] == 0) { frequency.erase(arr2[ptr]); } } else { int closestMatch = -1; int minDistance = INT_MAX; // Change minFrequency to minDistance for (auto it : frequency) { if (abs(arr2[ptr] - it.first) < minDistance) { // Change minFrequency to minDistance minDistance = abs(arr2[ptr] - it.first); // Change minFrequency to minDistance closestMatch = it.first; } } arr2[ptr] = closestMatch; } ptr++; } } int main() { vector<int> array1 = {1, 2, 3, 3, 5}; vector<int> array2 = {5, 4, 2, 6, 7, 8}; adjustArray(array1, array2); for (int num : array2) { cout << num << " "; } cout << endl; return 0; }
5 3 2 3 3 3
我们使用动态规划方法来寻找给定数组之间的最长公共子序列(LCS),以最小化对应索引处不相等元素的数量。为了跟踪两个数组中所有可能排列的元素的LCS长度,我们创建了一个二维表。为了减少差异,需要改变的元素可以通过回溯LCS来找到。通过修改LCS之外的元素以匹配LCS,确保数组之间更高的相似度。通过优化数组以共享一个公共子序列,这种动态规划技术有效地降低了不相等元素的数量。
Set the lengths of two arrays, dubbed array1 and array2, to m and n, respectively.
To store the LCS lengths for all possible combinations of elements from both arrays, create a 2D table DP of size (m 1) x (n 1).
使用两个已解决的循环来强调1和2号群集中的每个组件:
Set DP[i][j] = DP[i-1]. [j-1] 1 if the components on the current lists are the same.
On the off chance that the components vary, increment DP[i][j] to the most noteworthy conceivable esteem between DP[i-1][j] and DP[i][j-1].
Follow the LCS backwards from DP[m][n] to DP[0][0]:
如果array1[i-1]和array2[j-1]的元素被提升了,将array1[i-1]的角落移动到DP[i-1][j-1]并将array1[i-1]合并到最长公共子序列中
Elements outside the LCS in both arrays must be changed to match the LCS after tracing back the LCS in order to reduce the number of unequal elements.
The similitude of the adjusted arrays will increment, and the number of unequal components when comparing lists will diminish.
#include <iostream> #include <vector> using namespace std; vector<int> findLCS(vector<int>& array1, vector<int>& array2) { return {}; } int minimizeUnequalCount(vector<int>& array1, vector<int>& array2) { return 0; } void modifyArrays(vector<int>& array1, vector<int>& array2) { } int main() { vector<int> array1 = {1, 3, 5, 7, 9}; vector<int> array2 = {2, 4, 5, 8, 9}; vector<int> lcs = findLCS(array1, array2); cout << "Longest Common Subsequence: "; for (int num : lcs) { cout << num << " "; } cout << endl; int unequalCount = minimizeUnequalCount(array1, array2); cout << "Count of Unequal Elements after adjustment: " << unequalCount << endl; modifyArrays(array1, array2); cout << "Modified Array 1: "; for (int num : array1) { cout << num << " "; } cout << endl; cout << "Modified Array 2: "; for (int num : array2) { cout << num << " "; } cout << endl; return 0; }
Longest Common Subsequence: Count of Unequal Elements after adjustment: 0 Modified Array 1: 1 3 5 7 9 Modified Array 2: 2 4 5 8 9
有两种技术可用于减少两个给定数组之间对应索引处不相等元素的数量:哈希方法和排序方法。哈希方法为一个数组构建哈希表,并迭代地用哈希表中找到的最接近的匹配替换另一个数组中的元素。对于平均和最佳情况,这将实现O(N)的线性时间复杂度。另一方面,排序方法在迭代两个数组时按升序对它们进行排序,并将元素调整为较小的值。尽管它可能不总是产生最佳结果,但它使数组更具可比性。这两种方法都成功地减少了不一致元素的数量,增加了数组的相似性,并降低了对应位置的不一致元素的总数。
The above is the detailed content of Minimize the number of unequal elements at corresponding indices between the given arrays. For more information, please follow other related articles on the PHP Chinese website!