0

0

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

王林

王林

发布时间:2023-08-26 12:57:18

|

1402人浏览过

|

来源于tutorialspoint

转载

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

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 << num << " ";
   }
   cout << endl;

   return 0;
}

输出

5 3 2 3 3 3 

Maximum Independent Set (MIS) Approach

我们使用动态规划方法来寻找给定数组之间的最长公共子序列(LCS),以最小化对应索引处不相等元素的数量。为了跟踪两个数组中所有可能排列的元素的LCS长度,我们创建了一个二维表。为了减少差异,需要改变的元素可以通过回溯LCS来找到。通过修改LCS之外的元素以匹配LCS,确保数组之间更高的相似度。通过优化数组以共享一个公共子序列,这种动态规划技术有效地降低了不相等元素的数量。

Algorithm

  • 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号群集中的每个组件:

    Replit Ghostwrite
    Replit Ghostwrite

    一种基于 ML 的工具,可提供代码完成、生成、转换和编辑器内搜索功能。

    下载
    • 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]合并到最长公共子序列中

    • 根据DP中哪个尊重度更高,移动到已清空的DP[i][j-1]或者向上移动到DP[i-1][j](如果它们发生了变化)
  • 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.

Example

#include 
#include 
using namespace std;

vector findLCS(vector& array1, vector& array2) {
   return {};
}

int minimizeUnequalCount(vector& array1, vector& array2) {
   return 0;
}

void modifyArrays(vector& array1, vector& array2) {
}

int main() {
   vector array1 = {1, 3, 5, 7, 9};
   vector array2 = {2, 4, 5, 8, 9};

   vector 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 

Conclusion

有两种技术可用于减少两个给定数组之间对应索引处不相等元素的数量:哈希方法和排序方法。哈希方法为一个数组构建哈希表,并迭代地用哈希表中找到的最接近的匹配替换另一个数组中的元素。对于平均和最佳情况,这将实现O(N)的线性时间复杂度。另一方面,排序方法在迭代两个数组时按升序对它们进行排序,并将元素调整为较小的值。尽管它可能不总是产生最佳结果,但它使数组更具可比性。这两种方法都成功地减少了不一致元素的数量,增加了数组的相似性,并降低了对应位置的不一致元素的总数。

相关专题

更多
菜鸟裹裹入口以及教程汇总
菜鸟裹裹入口以及教程汇总

本专题整合了菜鸟裹裹入口地址及教程分享,阅读专题下面的文章了解更多详细内容。

0

2026.01.22

Golang 性能分析与pprof调优实战
Golang 性能分析与pprof调优实战

本专题系统讲解 Golang 应用的性能分析与调优方法,重点覆盖 pprof 的使用方式,包括 CPU、内存、阻塞与 goroutine 分析,火焰图解读,常见性能瓶颈定位思路,以及在真实项目中进行针对性优化的实践技巧。通过案例讲解,帮助开发者掌握 用数据驱动的方式持续提升 Go 程序性能与稳定性。

9

2026.01.22

html编辑相关教程合集
html编辑相关教程合集

本专题整合了html编辑相关教程合集,阅读专题下面的文章了解更多详细内容。

56

2026.01.21

三角洲入口地址合集
三角洲入口地址合集

本专题整合了三角洲入口地址合集,阅读专题下面的文章了解更多详细内容。

51

2026.01.21

AO3中文版入口地址大全
AO3中文版入口地址大全

本专题整合了AO3中文版入口地址大全,阅读专题下面的的文章了解更多详细内容。

397

2026.01.21

妖精漫画入口地址合集
妖精漫画入口地址合集

本专题整合了妖精漫画入口地址合集,阅读专题下面的文章了解更多详细内容。

118

2026.01.21

java版本选择建议
java版本选择建议

本专题整合了java版本相关合集,阅读专题下面的文章了解更多详细内容。

3

2026.01.21

Java编译相关教程合集
Java编译相关教程合集

本专题整合了Java编译相关教程,阅读专题下面的文章了解更多详细内容。

16

2026.01.21

C++多线程相关合集
C++多线程相关合集

本专题整合了C++多线程相关教程,阅读专题下面的的文章了解更多详细内容。

11

2026.01.21

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Django 教程
Django 教程

共28课时 | 3.4万人学习

Excel 教程
Excel 教程

共162课时 | 13万人学习

MongoDB 教程
MongoDB 教程

共17课时 | 2.2万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号