3132. Find the Integer Added to Array II

You are given two integer arrays nums1 and nums2.

From nums1 two elements have been removed, and all other elements have been increased (or decreased in the case of negative) by an integer, represented by the variable x.

As a result, nums1 becomes equal to nums2. Two arrays are considered equal when they contain the same integers with the same frequencies.

Return the minimum possible integerxthat achieves this equivalence.

Example 1:

Input: nums1 = [4,20,16,12,8], nums2 = [14,18,10]

Output: -2

Explanation:

After removing elements at indices [0,4] and adding -2, nums1 becomes [18,14,10].

Example 2:

Input: nums1 = [3,5,5,3], nums2 = [7,7]

Output: 2

Explanation:

After removing elements at indices [0,3] and adding 2, nums1 becomes [7,7].

Constraints:

  • 3 <= nums1.length <= 200
  • nums2.length == nums1.length - 2
  • 0 <= nums1[i], nums2[i] <= 1000
  • The test cases are generated in a way that there is an integer x such that nums1 can become equal to nums2 by removing two elements and adding x to each element of nums1.
cpp 复制代码
class Solution {
public:
    int minimumAddedInteger(vector<int>& nums1, vector<int>& nums2) {
        sort(nums1.begin(), nums1.end());
        sort(nums2.begin(), nums2.end());
         for (int i = 2; i > 0; i--) {
            int x = nums2[0] - nums1[i];
            int j = 0;
            for (int k = i; k < nums1.size(); k++) {
                if (nums2[j] == nums1[k] + x && ++j == nums2.size()) {
                    return x;
                }
            }
        }
        return nums2[0] - nums1[0];
    }
};
相关推荐
feifeigo12321 分钟前
MATLAB实现两组点云ICP配准
开发语言·算法·matlab
fengfuyao98524 分钟前
粒子群算法(PSO)求解标准VRP问题的MATLAB实现
开发语言·算法·matlab
Ayanami_Reii37 分钟前
进阶数据结构应用-SPOJ 3267 D-query
数据结构·算法·线段树·主席树·持久化线段树
guygg881 小时前
基于全变差的压缩感知视频图像重构算法
算法·重构·音视频
VT LI1 小时前
SDF在实时图形渲染中的核心原理与架构创新
算法·sdf·有号距离场
想七想八不如114081 小时前
408操作系统 PV专题
开发语言·算法
天一生水water1 小时前
储层认知→技术落地→产量优化
人工智能·算法·机器学习
明洞日记2 小时前
【VTK手册019】 深入理解 vtkProperty:从几何表达到 PBR 物理渲染
c++·图像处理·算法·vtk·图形渲染
Genevieve_xiao2 小时前
【数据结构与算法】【xjtuse】面向考纲学习(下)
java·数据结构·学习·算法
修炼地2 小时前
代码随想录算法训练营第二十七天 | 56. 合并区间、738.单调递增的数字、968.监控二叉树
c++·算法