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

Maximum Independent Set (MIS) Approach

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

letterdrop
letterdrop

B2B内容营销自动化平台,从创意到产生潜在客户的内容的最佳实践和工具。

下载

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

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

Conclusion

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

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
Kotlin Android模块化架构与组件化开发实践
Kotlin Android模块化架构与组件化开发实践

本专题围绕 Kotlin 在 Android 应用开发中的架构实践展开,重点讲解模块化设计与组件化开发的实现思路。内容包括项目模块拆分策略、公共组件封装、依赖管理优化、路由通信机制以及大型项目的工程化管理方法。通过真实项目案例分析,帮助开发者构建结构清晰、易扩展且维护成本低的 Android 应用架构体系,提升团队协作效率与项目迭代速度。

6

2026.03.09

JavaScript浏览器渲染机制与前端性能优化实践
JavaScript浏览器渲染机制与前端性能优化实践

本专题围绕 JavaScript 在浏览器中的执行与渲染机制展开,系统讲解 DOM 构建、CSSOM 解析、重排与重绘原理,以及关键渲染路径优化方法。内容涵盖事件循环机制、异步任务调度、资源加载优化、代码拆分与懒加载等性能优化策略。通过真实前端项目案例,帮助开发者理解浏览器底层工作原理,并掌握提升网页加载速度与交互体验的实用技巧。

60

2026.03.06

Rust内存安全机制与所有权模型深度实践
Rust内存安全机制与所有权模型深度实践

本专题围绕 Rust 语言核心特性展开,深入讲解所有权机制、借用规则、生命周期管理以及智能指针等关键概念。通过系统级开发案例,分析内存安全保障原理与零成本抽象优势,并结合并发场景讲解 Send 与 Sync 特性实现机制。帮助开发者真正理解 Rust 的设计哲学,掌握在高性能与安全性并重场景中的工程实践能力。

148

2026.03.05

PHP高性能API设计与Laravel服务架构实践
PHP高性能API设计与Laravel服务架构实践

本专题围绕 PHP 在现代 Web 后端开发中的高性能实践展开,重点讲解基于 Laravel 框架构建可扩展 API 服务的核心方法。内容涵盖路由与中间件机制、服务容器与依赖注入、接口版本管理、缓存策略设计以及队列异步处理方案。同时结合高并发场景,深入分析性能瓶颈定位与优化思路,帮助开发者构建稳定、高效、易维护的 PHP 后端服务体系。

273

2026.03.04

AI安装教程大全
AI安装教程大全

2026最全AI工具安装教程专题:包含各版本AI绘图、AI视频、智能办公软件的本地化部署手册。全篇零基础友好,附带最新模型下载地址、一键安装脚本及常见报错修复方案。每日更新,收藏这一篇就够了,让AI安装不再报错!

113

2026.03.04

Swift iOS架构设计与MVVM模式实战
Swift iOS架构设计与MVVM模式实战

本专题聚焦 Swift 在 iOS 应用架构设计中的实践,系统讲解 MVVM 模式的核心思想、数据绑定机制、模块拆分策略以及组件化开发方法。内容涵盖网络层封装、状态管理、依赖注入与性能优化技巧。通过完整项目案例,帮助开发者构建结构清晰、可维护性强的 iOS 应用架构体系。

159

2026.03.03

C++高性能网络编程与Reactor模型实践
C++高性能网络编程与Reactor模型实践

本专题围绕 C++ 在高性能网络服务开发中的应用展开,深入讲解 Socket 编程、多路复用机制、Reactor 模型设计原理以及线程池协作策略。内容涵盖 epoll 实现机制、内存管理优化、连接管理策略与高并发场景下的性能调优方法。通过构建高并发网络服务器实战案例,帮助开发者掌握 C++ 在底层系统与网络通信领域的核心技术。

30

2026.03.03

Golang 测试体系与代码质量保障:工程级可靠性建设
Golang 测试体系与代码质量保障:工程级可靠性建设

Go语言测试体系与代码质量保障聚焦于构建工程级可靠性系统。本专题深入解析Go的测试工具链(如go test)、单元测试、集成测试及端到端测试实践,结合代码覆盖率分析、静态代码扫描(如go vet)和动态分析工具,建立全链路质量监控机制。通过自动化测试框架、持续集成(CI)流水线配置及代码审查规范,实现测试用例管理、缺陷追踪与质量门禁控制,确保代码健壮性与可维护性,为高可靠性工程系统提供质量保障。

81

2026.02.28

Golang 工程化架构设计:可维护与可演进系统构建
Golang 工程化架构设计:可维护与可演进系统构建

Go语言工程化架构设计专注于构建高可维护性、可演进的企业级系统。本专题深入探讨Go项目的目录结构设计、模块划分、依赖管理等核心架构原则,涵盖微服务架构、领域驱动设计(DDD)在Go中的实践应用。通过实战案例解析接口抽象、错误处理、配置管理、日志监控等关键工程化技术,帮助开发者掌握构建稳定、可扩展Go应用的最佳实践方法。

62

2026.02.28

热门下载

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

精品课程

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

共28课时 | 4.9万人学习

Excel 教程
Excel 教程

共162课时 | 20.7万人学习

MongoDB 教程
MongoDB 教程

共17课时 | 3.2万人学习

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

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