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

    Napkin AI
    Napkin AI

    Napkin AI 可以将您的文本转换为图表、流程图、信息图、思维导图视觉效果,以便快速有效地分享您的想法。

    下载
    • 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)的线性时间复杂度。另一方面,排序方法在迭代两个数组时按升序对它们进行排序,并将元素调整为较小的值。尽管它可能不总是产生最佳结果,但它使数组更具可比性。这两种方法都成功地减少了不一致元素的数量,增加了数组的相似性,并降低了对应位置的不一致元素的总数。

相关专题

更多
Golang gRPC 服务开发与Protobuf实战
Golang gRPC 服务开发与Protobuf实战

本专题系统讲解 Golang 在 gRPC 服务开发中的完整实践,涵盖 Protobuf 定义与代码生成、gRPC 服务端与客户端实现、流式 RPC(Unary/Server/Client/Bidirectional)、错误处理、拦截器、中间件以及与 HTTP/REST 的对接方案。通过实际案例,帮助学习者掌握 使用 Go 构建高性能、强类型、可扩展的 RPC 服务体系,适用于微服务与内部系统通信场景。

8

2026.01.15

公务员递补名单公布时间 公务员递补要求
公务员递补名单公布时间 公务员递补要求

公务员递补名单公布时间不固定,通常在面试前,由招录单位(如国家知识产权局、海关等)发布,依据是原入围考生放弃资格,会按笔试成绩从高到低递补,递补考生需按公告要求限时确认并提交材料,及时参加面试/体检等后续环节。要求核心是按招录单位公告及时响应、提交材料(确认书、资格复审材料)并准时参加面试。

44

2026.01.15

公务员调剂条件 2026调剂公告时间
公务员调剂条件 2026调剂公告时间

(一)符合拟调剂职位所要求的资格条件。 (二)公共科目笔试成绩同时达到拟调剂职位和原报考职位的合格分数线,且考试类别相同。 拟调剂职位设置了专业科目笔试条件的,专业科目笔试成绩还须同时达到合格分数线,且考试类别相同。 (三)未进入原报考职位面试人员名单。

58

2026.01.15

国考成绩查询入口 国考分数公布时间2026
国考成绩查询入口 国考分数公布时间2026

笔试成绩查询入口已开通,考生可登录国家公务员局中央机关及其直属机构2026年度考试录用公务员专题网站http://bm.scs.gov.cn/pp/gkweb/core/web/ui/business/examResult/written_result.html,查询笔试成绩和合格分数线,点击“笔试成绩查询”按钮,凭借身份证及准考证进行查询。

11

2026.01.15

Java 桌面应用开发(JavaFX 实战)
Java 桌面应用开发(JavaFX 实战)

本专题系统讲解 Java 在桌面应用开发领域的实战应用,重点围绕 JavaFX 框架,涵盖界面布局、控件使用、事件处理、FXML、样式美化(CSS)、多线程与UI响应优化,以及桌面应用的打包与发布。通过完整示例项目,帮助学习者掌握 使用 Java 构建现代化、跨平台桌面应用程序的核心能力。

65

2026.01.14

php与html混编教程大全
php与html混编教程大全

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

36

2026.01.13

PHP 高性能
PHP 高性能

本专题整合了PHP高性能相关教程大全,阅读专题下面的文章了解更多详细内容。

75

2026.01.13

MySQL数据库报错常见问题及解决方法大全
MySQL数据库报错常见问题及解决方法大全

本专题整合了MySQL数据库报错常见问题及解决方法,阅读专题下面的文章了解更多详细内容。

21

2026.01.13

PHP 文件上传
PHP 文件上传

本专题整合了PHP实现文件上传相关教程,阅读专题下面的文章了解更多详细内容。

35

2026.01.13

热门下载

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

精品课程

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

共28课时 | 3.1万人学习

Excel 教程
Excel 教程

共162课时 | 11.9万人学习

MongoDB 教程
MongoDB 教程

共17课时 | 2万人学习

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

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