-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path1818.绝对差值和.js
127 lines (122 loc) · 2.94 KB
/
1818.绝对差值和.js
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
* @lc app=leetcode.cn id=1818 lang=javascript
*
* [1818] 绝对差值和
*/
// @lc code=start
/**
* @param {number[]} nums1
* @param {number[]} nums2
* @return {number}
*/
function Bisect() {
return { insort_right, insort_left, bisect_left, bisect_right }
function insort_right(a, x, lo = 0, hi = null) {
lo = bisect_right(a, x, lo, hi);
a.splice(lo, 0, x);
}
function bisect_right(a, x, lo = 0, hi = null) {
if (lo < 0) throw new Error('lo must be non-negative');
if (hi == null) hi = a.length;
while (lo < hi) {
let mid = lo + hi >> 1;
x < a[mid] ? hi = mid : lo = mid + 1;
}
return lo;
}
function insort_left(a, x, lo = 0, hi = null) {
lo = bisect_left(a, x, lo, hi);
a.splice(lo, 0, x);
}
function bisect_left(a, x, lo = 0, hi = null) {
if (lo < 0) throw new Error('lo must be non-negative');
if (hi == null) hi = a.length;
while (lo < hi) {
let mid = lo + hi >> 1;
a[mid] < x ? lo = mid + 1 : hi = mid;
}
return lo;
}
}
function TreeSet(elements) {
let ts = [];
let se = new Set();
let bisect = new Bisect();
if (elements) addAll(elements);
return { add, floor, ceiling, lower, remove, contains, size, clear, toArray };
function addAll(elements) {
for (const e of elements) {
if (se.has(e)) continue;
add(e);
se.add(e);
}
}
function add(e) {
if (!se.has(e)) {
bisect.insort_right(ts, e);
se.add(e);
}
}
function ceiling(e) {
let idx = bisect.bisect_right(ts, e);
if (ts[idx - 1] == e) return e;
return ts[bisect.bisect_right(ts, e)];
}
function floor(e) {
let idx = bisect.bisect_left(ts, e);
if (ts[idx] == e) {
return e;
} else {
return ts[bisect.bisect_left(ts, e) - 1];
}
}
function lower(e) {
let idx = bisect.bisect_left(ts, e);
if (ts[idx] < e) {
return ts[idx];
} else {
return ts[bisect.bisect_left(ts, e) - 1];
}
}
function remove(e) {
let res = new Set(ts);
res.delete(e);
ts = [...res];
se.delete(e);
}
function contains(e) {
return se.has(e);
}
function size() {
return ts.length;
}
function clear() {
ts = [];
}
function toArray() {
return ts;
}
}
const MOD = 1e9 + 7;
const MAX = Number.MAX_SAFE_INTEGER;
const MIN = Number.MIN_SAFE_INTEGER;
const mi = Math.min;
const abs = Math.abs;
const minAbsoluteSumDiff = (a, b) => {
let ts = new TreeSet([MIN, MAX]);
let res = sum = 0;
let n = a.length;
for (let i = 0; i < n; i++) {
ts.add(a[i]);
sum += abs(a[i] - b[i]);
}
res = sum;
for (let i = 0; i < n; i++) {
let it = ts.ceiling(b[i]);
res = mi(res, sum - abs(a[i] - b[i]) + abs(it - b[i]));
it = ts.lower(it);
res = mi(res, sum - abs(a[i] - b[i]) + abs(it - b[i]));
}
return res % MOD;
};
// @lc code=end