Given two arrays A, B with size n, you could justify the positions of number in array B to maximum the sum[abs(ai-bi)], i from 0 to n or minimize the sum[abs(ai-bi)].
---------------------------------------------------------------------
Maximum: sort all numbers in A and B, Use the last n numbers to minus the first n numbers.
Minimum: sort A and B separately. Use proof by contradiction:
Image Ai < Aj and Bi > Bj, discuss the6 situations:
Ai < Aj < Bi < Bj
Ai < Bi< Aj < Bj
Ai < Bi < Bj< Aj
Bi < Ai < Aj < Bj
Bi < Ai < Bj < Aj
Bi < Bj < Ai < Aj
In all of the situations , |Ai-Bi|+|Aj-Bj| <= |Ai-Bj|+|Aj-Bi|
时间: 2024-10-10 11:30:42