forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminimum-absolute-sum-difference.cpp
More file actions
28 lines (26 loc) · 983 Bytes
/
minimum-absolute-sum-difference.cpp
File metadata and controls
28 lines (26 loc) · 983 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Time: O(nlogn)
// Space: O(n)
class Solution {
public:
int minAbsoluteSumDiff(vector<int>& nums1, vector<int>& nums2) {
static const int MOD = 1e9 + 7;
vector<int> sorted_nums1(cbegin(nums1), cend(nums1));
sort(begin(sorted_nums1), end(sorted_nums1));
int result = 0, max_change = 0;
for (int i = 0; i < size(nums2); ++i) {
int diff = abs(nums1[i] - nums2[i]);
result = (result + diff) % MOD;
if (diff < max_change) {
continue;
}
const auto cit = lower_bound(cbegin(sorted_nums1), cend(sorted_nums1), nums2[i]);
if (cit != cend(sorted_nums1)) {
max_change = max(max_change, diff - abs(*cit - nums2[i]));
}
if (cit != cbegin(sorted_nums1)) {
max_change = max(max_change, diff - abs(*prev(cit) - nums2[i]));
}
}
return (result - max_change + MOD) % MOD;
}
};