给定分别为A , B和C的三个数组a [] , b []和c [] ,任务是找到abs(a [i] – b [j])+ abs(b [ j] – c [k]) ,其中0≤i≤A , 0≤j≤B和0≤k≤C 。
例子:
Input: A = 3, B = 2, C = 2, a[] = {1, 8, 5}, b[] = {2, 9}, c[] = {5, 4}
Output: 3
Explanation:
The triplet (a[0], b[0], c[1]), i.e. (1, 2, 4) has minimum sum of absolute difference of pairs, i.e. abs(1 – 2) + abs(2 – 4) = 1 + 2 = 3.
Input: A = 4, B = 3, C = 3, a[] = {4, 5, 1, 7}, b[] = {8, 5, 6}, c[] = {2, 7, 12}
Output: 2
Explanation:
The triplet (a[1], b[1], c[1]), i.e. (1, 5, 7) has minimum sum of absolute difference of pairs, i.e. abs(5 – 5) + abs(5 – 7) = 0 + 2 = 2.
方法:解决此问题的想法是对数组a []和c []进行排序,然后遍历数组b []并找到满足给定条件的元素。
请按照以下步骤解决问题:
- 将变量min初始化为INT_MAX ,以存储可能的最小值。
- 按升序对数组a []和c []进行排序。
- 遍历数组b [] ,对于每个元素,例如b [i] ,从数组a []和c []中找到最接近b [i]的元素作为arr_close和crr_close并执行以下操作:
- 为了找到最接近的元素,首先找到目标元素b [i]的lower_bound 。
- 如果找到下限,请检查它是否是数组的第一个元素。如果不是,则将下界及其先前的元素与目标元素进行比较,找到最接近目标元素的元素。
- 如果找不到下限,则最接近的元素将是数组的最后一个元素。
- 将min更新为abs(b [i] – arr_close)+ abs(b [i] – crr_close)的最小值。
- 完成上述步骤后,打印min的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
#include
using namespace std;
// Function to find the value
// closest to K in the array A[]
int closestValue(vector A, int k)
{
// Initialize close value as the
// end element
int close = A.back();
// Find lower bound of the array
auto it = lower_bound(A.begin(),
A.end(), k);
// If lower_bound is found
if (it != A.end()) {
close = *it;
// If lower_bound is not
// the first array element
if (it != A.begin()) {
// If *(it - 1) is closer to k
if ((k - *(it - 1))
< (close - k)) {
close = *(it - 1);
}
}
}
// Return closest value of k
return close;
}
// Function to find the minimum sum of
// abs(arr[i] - brr[j]) and abs(brr[j]-crr[k])
void minPossible(vector arr,
vector brr,
vector crr)
{
// Sort the vectors arr and crr
sort(arr.begin(), arr.end());
sort(crr.begin(), crr.end());
// Initialize minimum as INT_MAX
int minimum = INT_MAX;
// Traverse the array brr[]
for (int val : brr) {
// Stores the element closest
// to val from the array arr[]
int arr_close = closestValue(arr, val);
// Stores the element closest
// to val from the array crr[]
int crr_close = closestValue(crr, val);
// If sum of differences is minimum
if (abs(val - arr_close)
+ abs(val - crr_close)
< minimum)
// Update the minimum
minimum = abs(val - arr_close)
+ abs(val - crr_close);
}
// Print the minimum absolute
// difference possible
cout << minimum;
}
// Driver Code
int main()
{
vector a = { 1, 8, 5 };
vector b = { 2, 9 };
vector c = { 5, 4 };
// Function Call
minPossible(a, b, c);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
// Lower_bound function
public static int lower_bound(int arr[], int key)
{
int low = 0;
int high = arr.length - 1;
while (low < high)
{
int mid = low + (high - low) / 2;
if (arr[mid] >= key)
{
high = mid;
}
else
{
low = mid + 1;
}
}
return low;
}
// Function to find the value
// closest to K in the array A[]
static int closestValue(int A[], int k)
{
// Initialize close value as the
// end element
int close = A[A.length - 1];
// Find lower bound of the array
int it = lower_bound(A, k);
// If lower_bound is found
if (it != A.length)
{
close = A[it];
// If lower_bound is not
// the first array element
if (it != 0)
{
// If *(it - 1) is closer to k
if ((k - A[it - 1]) < (close - k))
{
close = A[it - 1];
}
}
}
// Return closest value of k
return close;
}
// Function to find the minimum sum of
// abs(arr[i] - brr[j]) and abs(brr[j]-crr[k])
static void minPossible(int arr[], int brr[],
int crr[])
{
// Sort the vectors arr and crr
Arrays.sort(arr);
Arrays.sort(crr);
// Initialize minimum as INT_MAX
int minimum = Integer.MAX_VALUE;
// Traverse the array brr[]
for(int val : brr)
{
// Stores the element closest
// to val from the array arr[]
int arr_close = closestValue(arr, val);
// Stores the element closest
// to val from the array crr[]
int crr_close = closestValue(crr, val);
// If sum of differences is minimum
if (Math.abs(val - arr_close) +
Math.abs(val - crr_close) < minimum)
// Update the minimum
minimum = Math.abs(val - arr_close) +
Math.abs(val - crr_close);
}
// Print the minimum absolute
// difference possible
System.out.println(minimum);
}
// Driver Code
public static void main(String[] args)
{
int a[] = { 1, 8, 5 };
int b[] = { 2, 9 };
int c[] = { 5, 4 };
// Function Call
minPossible(a, b, c);
}
}
// This code is contributed by Kingash
C#
// C# program for the above approach
using System;
class GFG{
// Lower_bound function
public static int lower_bound(int[] arr, int key)
{
int low = 0;
int high = arr.Length - 1;
while (low < high)
{
int mid = low + (high - low) / 2;
if (arr[mid] >= key)
{
high = mid;
}
else
{
low = mid + 1;
}
}
return low;
}
// Function to find the value
// closest to K in the array A[]
static int closestValue(int []A, int k)
{
// Initialize close value as the
// end element
int close = A[A.Length - 1];
// Find lower bound of the array
int it = lower_bound(A, k);
// If lower_bound is found
if (it != A.Length)
{
close = A[it];
// If lower_bound is not
// the first array element
if (it != 0)
{
// If *(it - 1) is closer to k
if ((k - A[it - 1]) < (close - k))
{
close = A[it - 1];
}
}
}
// Return closest value of k
return close;
}
// Function to find the minimum sum of
// abs(arr[i] - brr[j]) and abs(brr[j]-crr[k])
static void minPossible(int[] arr, int[] brr,
int[] crr)
{
// Sort the vectors arr and crr
Array.Sort(arr);
Array.Sort(crr);
// Initialize minimum as INT_MAX
int minimum = Int32.MaxValue;
// Traverse the array brr[]
foreach(int val in brr)
{
// Stores the element closest
// to val from the array arr[]
int arr_close = closestValue(arr, val);
// Stores the element closest
// to val from the array crr[]
int crr_close = closestValue(crr, val);
// If sum of differences is minimum
if (Math.Abs(val - arr_close) +
Math.Abs(val - crr_close) < minimum)
// Update the minimum
minimum = Math.Abs(val - arr_close) +
Math.Abs(val - crr_close);
}
// Print the minimum absolute
// difference possible
Console.WriteLine(minimum);
}
// Driver Code
static void Main()
{
int []a = { 1, 8, 5 };
int []b = { 2, 9 };
int []c = { 5, 4 };
// Function Call
minPossible(a, b, c);
}
}
// This code is contributed by SoumikMondal
3
时间复杂度: O(A * log A + C * log C + B)
辅助空间: O(A + B + C)