vczekilwid 发表于 2016-6-7 08:49:54

[Python]华为面试题,交换两个数组的元素使之总和的差值最小。

这是个NPC问题,所以就用穷举发了,在这里给出了一个用python的itertools的解法,个人认为比较简洁。

import itertools
def funcProduct(a, b):
for c in itertools.permutations(b):
for d in itertools.product(*):
yield zip(*d)
a =
b =
print min(funcProduct(a,b), key=lambda x:abs(sum(x)-sum(x)))
页: [1]
查看完整版本: [Python]华为面试题,交换两个数组的元素使之总和的差值最小。