当前位置:网站首页>leetcode 1818 绝对值,排序,二分法,最大值
leetcode 1818 绝对值,排序,二分法,最大值
2022-07-01 03:09:00 【风一样的航哥】
难度中等130收藏分享切换为英文接收动态反馈
给你两个正整数数组 nums1 和 nums2 ,数组的长度都是 n 。
数组 nums1 和 nums2 的 绝对差值和 定义为所有 |nums1[i] - nums2[i]|(0 <= i < n)的 总和(下标从 0 开始)。
你可以选用 nums1 中的 任意一个 元素来替换 nums1 中的 至多 一个元素,以 最小化 绝对差值和。
在替换数组 nums1 中最多一个元素 之后 ,返回最小绝对差值和。因为答案可能很大,所以需要对 109 + 7 取余 后返回。
|x| 定义为:
- 如果
x >= 0,值为x,或者 - 如果
x <= 0,值为-x
示例 1:
输入:nums1 = [1,7,5], nums2 = [2,3,5]
输出:3
解释:有两种可能的最优方案:
- 将第二个元素替换为第一个元素:[1,7,5] => [1,1,5] ,或者
- 将第二个元素替换为第三个元素:[1,7,5] => [1,5,5]
两种方案的绝对差值和都是 |1-2| + (|1-3| 或者 |5-3|) + |5-5| = 3
示例 2:
输入:nums1 = [2,4,6,8,10], nums2 = [2,4,6,8,10] 输出:0 解释:nums1 和 nums2 相等,所以不用替换元素。绝对差值和为 0
示例 3:
输入:nums1 = [1,10,4,4,2,7], nums2 = [9,3,5,1,7,4]
输出:20
解释:将第一个元素替换为第二个元素:[1,10,4,4,2,7] => [10,10,4,4,2,7]
绝对差值和为 |10-9| + |10-3| + |4-5| + |4-1| + |2-7| + |7-4| = 20
提示:
n == nums1.lengthn == nums2.length1 <= n <= 1051 <= nums1[i], nums2[i] <= 105
通过次数27,976提交次数74,953
这个题目,代码是想了一个小时想到的,题解为:记录一个差的绝对值数组diffnums,然后遍历数组2,找到如果数组1中替换某个数之后,产生的绝对值与对应的diffnums做差,寻找到最大的这个差值,即可,最后返回改变之后的和。
代码如下;
class Solution {
public:
int minAbsoluteSumDiff(vector<int>& nums1, vector<int>& nums2) {
vector<int> diffnums(nums1.size(),0);
long sum=0;
for(int i=0;i<nums1.size();i++)
{
diffnums[i]=abs(nums1[i]-nums2[i]);
sum+=diffnums[i];
sum %= 1000000007;
}
sort(nums1.begin(),nums1.end());
long maxvalue=0;
for(int i=0;i<nums2.size();i++)
{
int pos=lower_bound(nums1.begin(),nums1.end(),nums2[i])-nums1.begin();
if(pos<nums1.size())
{
maxvalue=max(maxvalue,(long)diffnums[i]-abs(nums1[pos]-nums2[i]));
}
if(pos>0)
{
maxvalue=max(maxvalue,(long)diffnums[i]-abs(nums1[pos-1]-nums2[i]));
}
maxvalue %= 1000000007;
}
return sum-maxvalue<0?sum-maxvalue+1000000007:sum-maxvalue;
}
};执行结果:
通过
显示详情
添加备注
执行用时:168 ms, 在所有 C++ 提交中击败了50.73%的用户
内存消耗:62.3 MB, 在所有 C++ 提交中击败了80.46%的用户
通过测试用例:51 / 51
边栏推荐
- HTB-Lame
- How to determine the progress bar loaded in the loading interface when opening the game
- Basic concept and classification of sorting
- C#实现图的深度优先遍历--非递归代码
- 终极套娃 2.0 | 云原生交付的封装
- Ctfshow blasting WP
- Why are strings immutable in many programming languages? [repeated] - why are strings immutable in many programming languages? [duplicate]
- Catch 222222
- gcc使用、Makefile总结
- Classic programming problem: finding the number of daffodils
猜你喜欢

How the network is connected: Chapter 2 (Part 2) packet receiving and sending operations between IP and Ethernet
![Od modify DLL and exe pop-up contents [OllyDbg]](/img/ff/7249e6e6644376ae048b23bf63b046.jpg)
Od modify DLL and exe pop-up contents [OllyDbg]

Example of Huawei operator level router configuration | example of configuring optionc mode cross domain LDP VPLS

servlet【初识】

Mysql知识点
![[QT] add knowledge supplement of third-party database](/img/ea/ca8b07ad80485208f2bb8ee8a78a28.png)
[QT] add knowledge supplement of third-party database

The best learning method in the world: Feynman learning method

lavaweb【初识后续问题的解决】

Const and the secret of pointers

Saving images of different depths in opencv
随机推荐
EtherCAT原理概述
IEDA 右键源码文件菜单简介
【机器学习】向量化计算 -- 机器学习路上必经路
pytest-fixture
性能测试常见面试题
雪崩问题以及sentinel的使用
JS to find duplicate elements in two arrays
Const and the secret of pointers
Hal library operation STM32 serial port
[linear DP] shortest editing distance
Best used trust automation script (shell)
PTA 1017
JUC学习
第03章_用户与权限管理
[machine learning] vectorized computing -- a must on the way of machine learning
Huawei operator level router configuration example | BGP VPLS configuration example
世界上最好的学习法:费曼学习法
An article explaining the publisher subscriber model and the observer model
Cloud native annual technology inventory is released! Ride the wind and waves at the right time
限流组件设计实战