当前位置:网站首页>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
边栏推荐
- 世界上最好的学习法:费曼学习法
- So easy deploy program to server
- Why are strings immutable in many programming languages? [repeated] - why are strings immutable in many programming languages? [duplicate]
- Metadata in NFT
- Subnet division and subnet summary
- 最好用的信任关系自动化脚本(shell)
- Cloud native annual technology inventory is released! Ride the wind and waves at the right time
- Pytest -- plug-in writing
- 【EXSI】主机间传输文件
- The best learning method in the world: Feynman learning method
猜你喜欢
Common interview questions for performance test

Huawei operator level router configuration example | configuration optionA mode cross domain LDP VPLS example

Restcloud ETL practice to realize incremental data synchronization without identification bit

Basic concepts of database

Cloud native annual technology inventory is released! Ride the wind and waves at the right time

Stop saying that you can't solve the "cross domain" problem

终极套娃 2.0 | 云原生交付的封装

Redis高效点赞与取消功能

最新接口自动化面试题

Huawei operator level router configuration example | configuration static VPLS example
随机推荐
JS日常开发小技巧(持续更新)
How to determine the progress bar loaded in the loading interface when opening the game
Hal library setting STM32 interrupt
Introduction to webrtc concept -- an article on understanding source, track, sink and mediastream
C language EXECL function
Poj-3486-computers[dynamic planning]
STM32 - DS18B20 temperature sampling of first-line protocol
Scale SVG to container without mask / crop
如何校验两个文件内容是否相同
Restcloud ETL practice to realize incremental data synchronization without identification bit
An article explaining the publisher subscriber model and the observer model
【小程序项目开发-- 京东商城】uni-app之首页商品楼层
Mysql知识点
UE4 rendering pipeline learning notes
So easy 将程序部署到服务器
Best used trust automation script (shell)
Dart training and sphygmomanometer inflation pump power control DPC
限流组件设计实战
So easy deploy program to server
JS to find duplicate elements in two arrays