将给定数组之间对应索引处的不相等元素的数量最小化

2023年 8月 29日 49.4k 0

将给定数组之间对应索引处的不相等元素的数量最小化

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.

Algorithm

  • 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.

Example

#include
#include
#include
#include
using namespace std;

void adjustArray(vector& arr1, vector& arr2) {
unordered_map 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 array1 = {1, 2, 3, 3, 5};
vector array2 = {5, 4, 2, 6, 7, 8};
adjustArray(array1, array2);

for (int num : array2) {
cout

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论