当前位置:网站首页>Leetcode 1818 absolute value, sorting, dichotomy, maximum value
Leetcode 1818 absolute value, sorting, dichotomy, maximum value
2022-07-01 03:19:00 【Air brother like the wind】
Medium difficulty 130 Switch to English to receive dynamic feedback
Here are two arrays of positive integers nums1 and nums2 , The length of the array is n .
Array nums1 and nums2 Of Absolute difference and Defined as all |nums1[i] - nums2[i]|(0 <= i < n) Of The sum of the ( Subscript from 0 Start ).
You can choose nums1 Medium Any one Element to replace nums1 Medium at most An element , With To minimize the Absolute difference and .
Replacing arrays nums1 At most one element in after , Returns the minimum absolute difference and . Because the answer can be big , So you need to 109 + 7 Remainder After the return .
|x| Defined as :
- If
x >= 0, The value isx, perhaps - If
x <= 0, The value is-x
Example 1:
Input :nums1 = [1,7,5], nums2 = [2,3,5]
Output :3
explain : There are two possible optimal solutions :
- Replace the second element with the first :[1,7,5] => [1,1,5] , perhaps
- Replace the second element with the third :[1,7,5] => [1,5,5]
The sum of absolute difference of the two schemes is |1-2| + (|1-3| perhaps |5-3|) + |5-5| = 3
Example 2:
Input :nums1 = [2,4,6,8,10], nums2 = [2,4,6,8,10] Output :0 explain :nums1 and nums2 equal , So you don't have to replace elements . The sum of absolute difference is 0
Example 3:
Input :nums1 = [1,10,4,4,2,7], nums2 = [9,3,5,1,7,4]
Output :20
explain : Replace the first element with the second :[1,10,4,4,2,7] => [10,10,4,4,2,7]
The sum of absolute difference is |10-9| + |10-3| + |4-5| + |4-1| + |2-7| + |7-4| = 20
Tips :
n == nums1.lengthn == nums2.length1 <= n <= 1051 <= nums1[i], nums2[i] <= 105
Pass times 27,976 Submit the number 74,953
This topic , The code came to mind after thinking for an hour , The answer to the question is : Record an array of absolute values of difference diffnums, Then iterate through the array 2, Find if array 1 After replacing a number in , The absolute value produced corresponds to diffnums Do a bad job , Find the maximum difference , that will do , Finally, return to the changed sum .
The code is as follows ;
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;
}
};Execution results :
adopt
According to the details
Add notes
Execution time :168 ms, In all C++ Defeated in submission 50.73% Users of
Memory consumption :62.3 MB, In all C++ Defeated in submission 80.46% Users of
Pass the test case :51 / 51
边栏推荐
- Cloud native annual technology inventory is released! Ride the wind and waves at the right time
- HTB-Lame
- Edge Drawing: A combined real-time edge and segment detector 翻译
- 【小程序项目开发 -- 京东商城】uni-app 商品分类页面(上)
- Nacos
- [applet project development -- JD mall] uni app commodity classification page (first)
- Force buckle - sum of two numbers
- STM32 - DS18B20 temperature sampling of first-line protocol
- Why are strings immutable in many programming languages? [repeated] - why are strings immutable in many programming languages? [duplicate]
- Error accessing URL 404
猜你喜欢

Kmeans

【小程序项目开发-- 京东商城】uni-app之分类导航区域

最新接口自动化面试题

8 pits of redis distributed lock

Detailed list of errors related to twincat3 ads of Beifu

限流组件设计实战

彻底解决Lost connection to MySQL server at ‘reading initial communication packet

Redis 教程

Keil5中如何做到 0 Error(s), 0 Warning(s).

E15 solution for cx5120 controlling Huichuan is620n servo error
随机推荐
Feign远程调用和Getaway网关
pytest-fixture
C#实现图的深度优先遍历--非递归代码
E15 solution for cx5120 controlling Huichuan is620n servo error
Cloud native annual technology inventory is released! Ride the wind and waves at the right time
C语言多线程编程入门学习笔记
md5sum操作
LeetCode_栈_困难_227.基本计算器(不含乘除)
About the application of MySQL
调试定位导航遇到的问题总结
Ctfshow blasting WP
Complete training and verification of a neural network based on pytorch
如果在小券商办理网上开户安全吗?我的资金会不会不安全?
LeetCode_ Stack_ Difficulties_ 227. basic calculator (excluding multiplication and division)
Golang多图生成gif
VMware vSphere 6.7虚拟化云管理之12、VCSA6.7更新vCenter Server许可
servlet【初识】
[linear DP] longest common subsequence
【机器学习】向量化计算 -- 机器学习路上必经路
How to verify whether the contents of two files are the same