首页 > 代码库 > 数组差的绝对值
数组差的绝对值
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|
数组差的绝对值
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。