📌  相关文章
📜  来自 N 个坐标的不同对之间的最大曼哈顿距离

📅  最后修改于: 2021-10-23 08:25:37             🧑  作者: Mango

给定一个由N 个整数坐标组成的数组arr[] ,任务是找到任何两个不同坐标对之间的最大曼哈顿距离。

例子:

朴素的方法:最简单的方法是迭代数组,对于每个坐标,计算它与所有剩余点的曼哈顿距离。每次计算后不断更新得到的最大距离。最后,打印得到的最大距离。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to calculate the maximum
// Manhattan distance
void MaxDist(vector >& A, int N)
{
    // Stores the maximum distance
    int maximum = INT_MIN;
 
    for (int i = 0; i < N; i++) {
 
        int sum = 0;
 
        for (int j = i + 1; j < N; j++) {
 
            // Find Manhattan distance
            // using the formula
            // |x1 - x2| + |y1 - y2|
            sum = abs(A[i].first - A[j].first)
                  + abs(A[i].second - A[j].second);
 
            // Updating the maximum
            maximum = max(maximum, sum);
        }
    }
 
    cout << maximum;
}
 
// Driver Code
int main()
{
    int N = 3;
 
    // Given Co-ordinates
    vector > A
        = { { 1, 2 }, { 2, 3 }, { 3, 4 } };
 
    // Function Call
    MaxDist(A, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
 
class GFG {
 
    // Pair class
    public static class Pair {
        int x;
        int y;
 
        Pair(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
    }
 
    // Function to calculate the maximum
    // Manhattan distance
    static void MaxDist(ArrayList A, int N)
    {
 
        // Stores the maximum distance
        int maximum = Integer.MIN_VALUE;
 
        for (int i = 0; i < N; i++) {
            int sum = 0;
 
            for (int j = i + 1; j < N; j++) {
 
                // Find Manhattan distance
                // using the formula
                // |x1 - x2| + |y1 - y2|
                sum = Math.abs(A.get(i).x - A.get(j).x)
                      + Math.abs(A.get(i).y - A.get(j).y);
 
                // Updating the maximum
                maximum = Math.max(maximum, sum);
            }
        }
        System.out.println(maximum);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int n = 3;
 
        ArrayList al = new ArrayList<>();
 
        // Given Co-ordinates
        Pair p1 = new Pair(1, 2);
        al.add(p1);
 
        Pair p2 = new Pair(2, 3);
        al.add(p2);
 
        Pair p3 = new Pair(3, 4);
        al.add(p3);
 
        // Function call
        MaxDist(al, n);
    }
}
 
// This code is contributed by bikram2001jha


Python3
# Python3 program for the above approach
import sys
 
# Function to calculate the maximum
# Manhattan distance
 
 
def MaxDist(A, N):
 
    # Stores the maximum distance
    maximum = - sys.maxsize
 
    for i in range(N):
        sum = 0
 
        for j in range(i + 1, N):
 
            # Find Manhattan distance
            # using the formula
            # |x1 - x2| + |y1 - y2|
            Sum = (abs(A[i][0] - A[j][0]) +
                   abs(A[i][1] - A[j][1]))
 
            # Updating the maximum
            maximum = max(maximum, Sum)
 
    print(maximum)
 
 
# Driver code
N = 3
 
# Given co-ordinates
A = [[1, 2], [2, 3], [3, 4]]
 
# Function call
MaxDist(A, N)
 
# This code is contributed by divyeshrabadiya07


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG {
 
    // Pair class
    public class Pair {
        public int x;
        public int y;
 
        public Pair(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
    }
 
    // Function to calculate the maximum
    // Manhattan distance
    static void MaxDist(List A, int N)
    {
 
        // Stores the maximum distance
        int maximum = int.MinValue;
 
        for (int i = 0; i < N; i++) {
            int sum = 0;
 
            for (int j = i + 1; j < N; j++) {
 
                // Find Manhattan distance
                // using the formula
                // |x1 - x2| + |y1 - y2|
                sum = Math.Abs(A[i].x - A[j].x)
                      + Math.Abs(A[i].y - A[j].y);
 
                // Updating the maximum
                maximum = Math.Max(maximum, sum);
            }
        }
        Console.WriteLine(maximum);
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        int n = 3;
 
        List al = new List();
 
        // Given Co-ordinates
        Pair p1 = new Pair(1, 2);
        al.Add(p1);
 
        Pair p2 = new Pair(2, 3);
        al.Add(p2);
 
        Pair p3 = new Pair(3, 4);
        al.Add(p3);
 
        // Function call
        MaxDist(al, n);
    }
}
 
// This code is contributed by Amit Katiyar


C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to calculate the maximum
// Manhattan distance
void MaxDist(vector >& A, int N)
{
    // Vectors to store maximum and
    // minimum of all the four forms
    vector V(N), V1(N);
 
    for (int i = 0; i < N; i++) {
        V[i] = A[i].first + A[i].second;
        V1[i] = A[i].first - A[i].second;
    }
 
    // Sorting both the vectors
    sort(V.begin(), V.end());
    sort(V1.begin(), V1.end());
 
    int maximum
        = max(V.back() - V.front(), V1.back() - V1.front());
 
    cout << maximum << endl;
}
 
// Driver Code
int main()
{
    int N = 3;
 
    // Given Co-ordinates
    vector > A
        = { { 1, 2 }, { 2, 3 }, { 3, 4 } };
 
    // Function Call
    MaxDist(A, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
 
class GFG {
 
    // Pair class
    public static class Pair {
        int x;
        int y;
 
        Pair(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
    }
 
    // Function to calculate the maximum
    // Manhattan distance
    static void MaxDist(ArrayList A, int N)
    {
 
        // ArrayLists to store maximum and
        // minimum of all the four forms
        ArrayList V = new ArrayList<>();
        ArrayList V1 = new ArrayList<>();
 
        for (int i = 0; i < N; i++) {
            V.add(A.get(i).x + A.get(i).y);
            V1.add(A.get(i).x - A.get(i).y);
        }
 
        // Sorting both the ArrayLists
        Collections.sort(V);
        Collections.sort(V1);
 
        int maximum
            = Math.max((V.get(V.size() - 1) - V.get(0)),
                       (V1.get(V1.size() - 1) - V1.get(0)));
 
        System.out.println(maximum);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int n = 3;
 
        ArrayList al = new ArrayList<>();
 
        // Given Co-ordinates
        Pair p1 = new Pair(1, 2);
        al.add(p1);
        Pair p2 = new Pair(2, 3);
        al.add(p2);
        Pair p3 = new Pair(3, 4);
        al.add(p3);
 
        // Function call
        MaxDist(al, n);
    }
}
 
// This code is contributed by bikram2001jha


Python3
# Python3 program for the above approach
 
# Function to calculate the maximum
# Manhattan distance
 
 
def MaxDist(A, N):
 
    # List to store maximum and
    # minimum of all the four forms
    V = [0 for i in range(N)]
    V1 = [0 for i in range(N)]
 
    for i in range(N):
        V[i] = A[i][0] + A[i][1]
        V1[i] = A[i][0] - A[i][1]
 
    # Sorting both the vectors
    V.sort()
    V1.sort()
 
    maximum = max(V[-1] - V[0],
                  V1[-1] - V1[0])
 
    print(maximum)
 
 
# Driver code
if __name__ == "__main__":
 
    N = 3
 
    # Given Co-ordinates
    A = [[1, 2],
         [2, 3],
         [3, 4]]
 
    # Function call
    MaxDist(A, N)
 
# This code is contributed by rutvik_56


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG {
 
    // Pair class
    class Pair {
        public int x;
        public int y;
 
        public Pair(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
    }
 
    // Function to calculate the maximum
    // Manhattan distance
    static void MaxDist(List A, int N)
    {
 
        // Lists to store maximum and
        // minimum of all the four forms
        List V = new List();
        List V1 = new List();
 
        for (int i = 0; i < N; i++) {
            V.Add(A[i].x + A[i].y);
            V1.Add(A[i].x - A[i].y);
        }
 
        // Sorting both the Lists
        V.Sort();
        V1.Sort();
 
        int maximum = Math.Max((V[V.Count - 1] - V[0]),
                               (V1[V1.Count - 1] - V1[0]));
 
        Console.WriteLine(maximum);
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        int n = 3;
 
        List al = new List();
 
        // Given Co-ordinates
        Pair p1 = new Pair(1, 2);
        al.Add(p1);
        Pair p2 = new Pair(2, 3);
        al.Add(p2);
        Pair p3 = new Pair(3, 4);
        al.Add(p3);
 
        // Function call
        MaxDist(al, n);
    }
}
 
// This code is contributed by Amit Katiyar


输出:
4

时间复杂度: O(N 2 ),其中 N 是给定数组的大小。
辅助空间: O(N)

有效的方法:这个想法是使用 X 和 Y 坐标之间的总和和差异,并通过对这些差异进行排序来找到答案。以下是对上述问题陈述的观察:

  • 任意两点(X i , Y i )(X j , Y j )之间的曼哈顿距离可以写成如下:
  • 上面的表达式可以重新排列为:
  • 从上面的表达式可以看出,通过存储坐标的和和差可以找到答案。

请按照以下步骤解决问题:

  1. 初始化两个数组sum[]diff[]
  2. XY坐标的总和,即X i + Y i 存储sum[] 中,并将它们的差值,即X i – Y i 存储diff[] 中
  3. 按升序对sum[]diff[]进行排序。
  4. 值 ( sum[N-1] – sum[0])(diff[N-1] – diff[0]) 中的最大值是所需的结果。

下面是上述方法的实现:

C++

// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to calculate the maximum
// Manhattan distance
void MaxDist(vector >& A, int N)
{
    // Vectors to store maximum and
    // minimum of all the four forms
    vector V(N), V1(N);
 
    for (int i = 0; i < N; i++) {
        V[i] = A[i].first + A[i].second;
        V1[i] = A[i].first - A[i].second;
    }
 
    // Sorting both the vectors
    sort(V.begin(), V.end());
    sort(V1.begin(), V1.end());
 
    int maximum
        = max(V.back() - V.front(), V1.back() - V1.front());
 
    cout << maximum << endl;
}
 
// Driver Code
int main()
{
    int N = 3;
 
    // Given Co-ordinates
    vector > A
        = { { 1, 2 }, { 2, 3 }, { 3, 4 } };
 
    // Function Call
    MaxDist(A, N);
 
    return 0;
}

Java

// Java program for the above approach
import java.io.*;
import java.util.*;
 
class GFG {
 
    // Pair class
    public static class Pair {
        int x;
        int y;
 
        Pair(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
    }
 
    // Function to calculate the maximum
    // Manhattan distance
    static void MaxDist(ArrayList A, int N)
    {
 
        // ArrayLists to store maximum and
        // minimum of all the four forms
        ArrayList V = new ArrayList<>();
        ArrayList V1 = new ArrayList<>();
 
        for (int i = 0; i < N; i++) {
            V.add(A.get(i).x + A.get(i).y);
            V1.add(A.get(i).x - A.get(i).y);
        }
 
        // Sorting both the ArrayLists
        Collections.sort(V);
        Collections.sort(V1);
 
        int maximum
            = Math.max((V.get(V.size() - 1) - V.get(0)),
                       (V1.get(V1.size() - 1) - V1.get(0)));
 
        System.out.println(maximum);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int n = 3;
 
        ArrayList al = new ArrayList<>();
 
        // Given Co-ordinates
        Pair p1 = new Pair(1, 2);
        al.add(p1);
        Pair p2 = new Pair(2, 3);
        al.add(p2);
        Pair p3 = new Pair(3, 4);
        al.add(p3);
 
        // Function call
        MaxDist(al, n);
    }
}
 
// This code is contributed by bikram2001jha

蟒蛇3

# Python3 program for the above approach
 
# Function to calculate the maximum
# Manhattan distance
 
 
def MaxDist(A, N):
 
    # List to store maximum and
    # minimum of all the four forms
    V = [0 for i in range(N)]
    V1 = [0 for i in range(N)]
 
    for i in range(N):
        V[i] = A[i][0] + A[i][1]
        V1[i] = A[i][0] - A[i][1]
 
    # Sorting both the vectors
    V.sort()
    V1.sort()
 
    maximum = max(V[-1] - V[0],
                  V1[-1] - V1[0])
 
    print(maximum)
 
 
# Driver code
if __name__ == "__main__":
 
    N = 3
 
    # Given Co-ordinates
    A = [[1, 2],
         [2, 3],
         [3, 4]]
 
    # Function call
    MaxDist(A, N)
 
# This code is contributed by rutvik_56

C#

// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG {
 
    // Pair class
    class Pair {
        public int x;
        public int y;
 
        public Pair(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
    }
 
    // Function to calculate the maximum
    // Manhattan distance
    static void MaxDist(List A, int N)
    {
 
        // Lists to store maximum and
        // minimum of all the four forms
        List V = new List();
        List V1 = new List();
 
        for (int i = 0; i < N; i++) {
            V.Add(A[i].x + A[i].y);
            V1.Add(A[i].x - A[i].y);
        }
 
        // Sorting both the Lists
        V.Sort();
        V1.Sort();
 
        int maximum = Math.Max((V[V.Count - 1] - V[0]),
                               (V1[V1.Count - 1] - V1[0]));
 
        Console.WriteLine(maximum);
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        int n = 3;
 
        List al = new List();
 
        // Given Co-ordinates
        Pair p1 = new Pair(1, 2);
        al.Add(p1);
        Pair p2 = new Pair(2, 3);
        al.Add(p2);
        Pair p3 = new Pair(3, 4);
        al.Add(p3);
 
        // Function call
        MaxDist(al, n);
    }
}
 
// This code is contributed by Amit Katiyar
输出:
4

时间复杂度: O(N*log N)
辅助空间: O(N)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程