题目标题

求三个升序数组a[l],b[m],c[n]中,每个数组出一个数,要求max(|a[i]-b[j]|,|a[i]-c[k]|,|b[j]-c[k]|)最小。

参考解析
  1. def mins(a,b,c):
  2. mins = a if a < b else b
  3. mins = mins if mins < c else c
  4. return mins
  5. def maxs(a,b,c):
  6. maxs = b if a < b else a
  7. maxs = c if maxs < c else maxs
  8. return maxs
  9. def minDistance(a,b,c):
  10. aLen = len(a)
  11. bLen = len(b)
  12. cLen = len(c)
  13. curDist = 0
  14. minsd = 0
  15. minDist = 2 ** 32
  16. i = 0 #数组a的下标
  17. j = 0 #数组b的下标
  18. k = 0 #数组c的下标
  19. while True:
  20. curDist = maxs(abs(a[i]-b[j]),abs(a[i]-c[k]),abs(b[j]-c[k]))
  21. if curDist < minDist:
  22. minDist = curDist
  23. #找出当前遍历到三个数组中最小值
  24. minsd = mins(a[i],b[j],c[k])
  25. if minsd == a[i]:
  26. i += 1
  27. if i >= aLen:
  28. break
  29. elif minsd == b[j]:
  30. j += 1
  31. if j >= bLen:
  32. break
  33. else:
  34. k += 1
  35. if k >= cLen:
  36. break
  37. return minDist
  38. if __name__ == "__main__":
  39. a = [3,4,5,7,15]
  40. b = [10,12,14,16,17]
  41. c = [20,21,23,24,37,30]
  42. print("最小距离为:"+str(minDistance(a,b,c)))