1818. 绝对差值和
为保证权益,题目请参考 1818. 绝对差值和(From LeetCode).
解决方案1
Python
python
# 1818. 绝对差值和
#
from typing import List
import bisect
class Solution:
def minAbsoluteSumDiff(self, nums1: List[int], nums2: List[int]) -> int:
ans = 0
nums1s = sorted(nums1)
maxn = 0
for i in range(len(nums1)):
diff = abs(nums1[i] - nums2[i])
ans += diff
j = bisect.bisect_left(nums1s, nums2[i])
if j < len(nums1s):
maxn = max(maxn, diff - abs(nums1s[j] - nums2[i]))
if j - 1 >= 0:
maxn = max(maxn, diff - abs(nums1s[j - 1] - nums2[i]))
return (ans - maxn + 1000000007) % 1000000007
if __name__ == "__main__":
so = Solution()
print(so.minAbsoluteSumDiff([2, 4, 6, 8, 10], [2, 4, 6, 8, 10]))
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
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