LintCode问题图解-14

本文准备讲解1个简单的算法编程问题, 这个算法编程问题来自LintCode平台。不了解.LintCode平台的读者可以阅读笔者文章(在线编程平台推荐-LeetCode)。问题的英文版本描述如下:

The Smallest Difference

Given two arrays of integers (the first array is arrayA, the second array is arrayB), now we are going to find an element in array A which is A[i], and another element in array B which is B[j], so that the difference between A[i] and B[j] (|A[i] - B[j]|) is as small as possible, return their smallest difference.

Example

For example, given array A =[3,6,7,4], B =[2,8,9,3], return 0.

最小差

给定两个整数数组(第一个是数组A,第二个是数组B),在数组 A 中取 A[i],数组 B 中取 B[j],A[i] 和 B[j]两者的差(|A[i] - B[j]|)为最小值。返回最小差。

样例

给定数组 A =[3,4,6,7], B =[2,3,8,9],返回0。

初步观察题目要求,设计算法方案有困难。对数组 A 的任意1个元素,数组 B 存在1个元素;数组 B 的这个元素与数组A 的目标元素比较差矩最小。为了高效寻找与数组A 的目标元素比较差矩最小的数组B 的元素,需要先对 数组 A 和数组 B 做排序算法处理。



LintCode问题图解-14_第1张图片
简单高效的算法

你可能感兴趣的:(LintCode问题图解-14)