给定一个包含N个点的数组arr ,任务是查找点等距的三元组总数。
A triplet of points (P1, P2, P3) is said to be equidistant when the distance between P1 and P2 is the same as of the distance between P1 and P3.
注意:点的顺序很重要,即(P1,P2,P3)与(P2,P3,P1)不同。
例子:
Input: arr = [[0, 0], [1, 0], [2, 0]]
Output: 2
Explanation:
Since the order of the points matters, we have two different sets of points [[1, 0], [0, 0], [2, 0]] and [[1, 0], [2, 0], [0, 0]] in which the points are equidistant.
Input: arr = [[1, 1], [1, 3], [2, 0]]
Output: 0
Explanation:
It is not possible to get any such triplet in which the points are equidistant.
方法:为解决上述问题,我们知道一个三重态的阶数,因此,一个三重态的排列可能会多个,并且满足等距点对的条件。
- 首先,我们将计算其中具有等距点的三元组的所有排列。
- 对列表中每个不同的三元组重复相同的过程。为了计算距离,我们将使用各个坐标之间的距离的平方。
- 使用哈希图可存储单个三元组的各种等距点对。
- 一旦我们计算了对的总数,就可以计算所需的排列。我们对所有不同的三元组重复此过程,并将所有排列添加到结果中。
下面是上述方法的实现:
C++
// C++ implementation to Find
// the total number of Triplets
// in which the points are Equidistant
#include
using namespace std;
// function to count such triplets
int numTrip(vector >& points)
{
int res = 0;
// Iterate over all the points
for (int i = 0; i < points.size(); ++i) {
unordered_map
map(points.size());
// Iterate over all points other
// than the current point
for (int j = 0; j < points.size(); ++j) {
if (j == i)
continue;
int dy = points[i].second
- points[j].second;
int dx = points[i].first
- points[j].first;
// Compute squared euclidean distance
// for the current point
int key = dy * dy;
key += dx * dx;
map[key]++;
}
for (auto& p : map)
// Compute nP2 that is n * (n - 1)
res += p.second * (p.second - 1);
}
// Return the final result
return res;
}
// Driver code
int main()
{
vector > mat
= { { 0, 0 }, { 1, 0 }, { 2, 0 } };
cout << numTrip(mat);
return 0;
}
Java
// Java implementation to find the total
// number of Triplets in which the
// points are Equidistant
import java.util.*;
@SuppressWarnings("unchecked")
class GFG{
static class pair
{
public int first, second;
public pair(int first, int second)
{
this.first = first;
this.second = second;
}
}
// Function to size() such triplets
static int numTrip(ArrayList points)
{
int res = 0;
// Iterate over all the points
for(int i = 0; i < points.size(); ++i)
{
HashMap map = new HashMap<>();
// Iterate over all points other
// than the current point
for(int j = 0; j < points.size(); ++j)
{
if (j == i)
continue;
int dy = ((pair)points.get(i)).second -
((pair)points.get(j)).second;
int dx = ((pair)points.get(i)).first -
((pair)points.get(j)).first;
// Compute squared euclidean distance
// for the current point
long key = dy * dy;
key += dx * dx;
if (map.containsKey(key))
{
map.put(key, map.get(key) + 1);
}
else
{
map.put(key, 1);
}
}
for(int p : map.values())
// Compute nP2 that is n * (n - 1)
res += p * (p - 1);
}
// Return the final result
return res;
}
// Driver code
public static void main(String []args)
{
ArrayList mat = new ArrayList();
mat.add(new pair(0, 0));
mat.add(new pair(1, 0));
mat.add(new pair(2, 0));
System.out.println(numTrip(mat));
}
}
// This code is contributed by rutvik_56
Python3
# Python3 implementation to find
# the total number of Triplets
# in which the points are Equidistant
# Function to count such triplets
def numTrip(points):
res = 0
# Iterate over all the points
for i in range(len(points)):
map = {}
# Iterate over all points other
# than the current point
for j in range(len(points)):
if (j == i):
continue
dy = points[i][1] - points[j][1]
dx = points[i][0] - points[j][0]
# Compute squared euclidean distance
# for the current point
key = dy * dy
key += dx * dx
map[key] = map.get(key, 0) + 1
for p in map:
# Compute nP2 that is n * (n - 1)
res += map[p] * (map[p] - 1)
# Return the final result
return res
# Driver code
if __name__ == '__main__':
mat = [ [ 0, 0 ],
[ 1, 0 ],
[ 2, 0 ] ]
print (numTrip(mat))
# This code is contributed by mohit kumar 29
C#
// C# implementation to find the total
// number of Triplets in which the
// points are Equidistant
using System;
using System.Collections;
using System.Collections.Generic;
class GFG{
class pair
{
public int first, second;
public pair(int first, int second)
{
this.first = first;
this.second = second;
}
}
// Function to count such triplets
static int numTrip(ArrayList points)
{
int res = 0;
// Iterate over all the points
for(int i = 0; i < points.Count; ++i)
{
Dictionary map = new Dictionary();
// Iterate over all points other
// than the current point
for(int j = 0; j < points.Count; ++j)
{
if (j == i)
continue;
int dy = ((pair)points[i]).second -
((pair)points[j]).second;
int dx = ((pair)points[i]).first -
((pair)points[j]).first;
// Compute squared euclidean distance
// for the current point
int key = dy * dy;
key += dx * dx;
if (map.ContainsKey(key))
{
map[key]++;
}
else
{
map[key] = 1;
}
}
foreach(int p in map.Values)
// Compute nP2 that is n * (n - 1)
res += p * (p - 1);
}
// Return the final result
return res;
}
// Driver code
public static void Main(string []args)
{
ArrayList mat = new ArrayList(){ new pair(0, 0),
new pair(1, 0),
new pair(2, 0) };
Console.Write(numTrip(mat));
}
}
// This code is contributed by pratham76
2
时间复杂度: O(N 2 )