当前位置:网站首页>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
边栏推荐
- 【读书笔记】《文案变现》——写出有效文案的四个黄金步骤
- 一文讲解发布者订阅者模式与观察者模式
- Hal library operation STM32 serial port
- Metadata in NFT
- The best learning method in the world: Feynman learning method
- Redis分布式锁的8大坑
- Add / delete / modify query summary insert/create/put/add/save/post, delete/drop/remove, update/modify/change, select/get/list/find
- 倍福TwinCAT3 Ads相关错误详细列表
- C # realize solving the shortest path of unauthorized graph based on breadth first BFS -- complete program display
- php批量excel转word
猜你喜欢
![[machine learning] vectorized computing -- a must on the way of machine learning](/img/3f/d672bb254f845ea705b3a0ca10ee19.png)
[machine learning] vectorized computing -- a must on the way of machine learning

STM32 - DS18B20 temperature sampling of first-line protocol

ctfshow爆破wp

安装VCenter6.7【VCSA6.7(vCenter Server Appliance 6.7) 】

Huawei operator level router configuration example | configuration static VPLS example

# 使用 KubeKey 搭建 Kubernetes/KubeSphere 环境的'心路(累)历程'

几行事务代码,让我赔了16万

Complete training and verification of a neural network based on pytorch

Detailed list of errors related to twincat3 ads of Beifu

XXL job User Guide
随机推荐
Huawei operator level router configuration example | configuration optionA mode cross domain LDP VPLS example
JUC学习
最好用的信任关系自动化脚本(shell)
彻底解决Lost connection to MySQL server at ‘reading initial communication packet
安装VCenter6.7【VCSA6.7(vCenter Server Appliance 6.7) 】
[applet project development -- Jingdong Mall] user defined search component of uni app (Part 1)
Classic programming problem: finding the number of daffodils
Servlet [first introduction]
Basic concepts of database
Is it safe to open an account online in a small securities firm? Will my money be unsafe?
Pytest -- plug-in writing
Borrowing constructor inheritance and composite inheritance
Subnet division and subnet summary
C # realize solving the shortest path of unauthorized graph based on breadth first BFS -- complete program display
Stop saying that you can't solve the "cross domain" problem
Elk elegant management server log
Design practice of current limiting components
LeetCode_ Stack_ Difficulties_ 227. basic calculator (excluding multiplication and division)
A few lines of transaction codes cost me 160000 yuan
pytest-fixture