给定X i和Y i在2-D平面中N个不同点的坐标,任务是计算可以由平行于X和Y轴的这些点形成的平方数。
例子:
Input : X[] = { 0, 2, 0, 2 }, Y[] = { 0, 2, 2, 0 }
Output : 1
Explanation: Only one Square can be formed using these points –
(0, 2)(2, 2)
(0, 0)(2, 0)
Input : X[] = { 3, 2, 0, 6 }, Y[] = { 0, 2, 0, 6 }
Output : 0
Explanation: No Square can be formed using these points –
(3,0),(2,0), (0,0), (6,6)
天真的方法:迭代四个点的所有可能组合,并检查是否可以形成平行于X轴和Y轴的正方形。该方法的时间复杂度为O(N 4 ) 。
高效的方法:我们可以观察到,对于要形成所需平方的任何四个点,必须满足以下条件–
因此,可以将任意正方形的四个点按顺时针方向分别写为P1(X 1 ,Y 1 ),P2(X 2 ,Y 1 ),P3(X 2 ,Y 2 )和P4(X 1 ,Y 2 )。
考虑给定点在同一水平或垂直线上的任何两个点。计算它们之间的距离。在此基础上,形成另外两点。现在检查两个给定的点是否都存在。如果是这样,则确保存在正方形。因此,增加计数并继续进行。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function that returns the count of
// squares parallel to both X and Y-axis
// from a given set of points
int countSquares(int* X, int* Y, int N)
{
// Initialize result
int count = 0;
// Initialize a set to store points
set > points;
// Initialize a map to store the
// points in the same vertical line
map > vertical;
// Store the points in a set
for (int i = 0; i < N; i++) {
points.insert({ X[i], Y[i] });
}
// Store the points in the same vertical line
// i.e. with same X co-ordinates
for (int i = 0; i < N; i++) {
vertical[X[i]].push_back(Y[i]);
}
// Check for every two points
// in the same vertical line
for (auto line : vertical) {
int X1 = line.first;
vector yList = line.second;
for (int i = 0; i < yList.size(); i++) {
int Y1 = yList[i];
for (int j = i + 1; j < yList.size(); j++) {
int Y2 = yList[j];
int side = abs(Y1 - Y2);
int X2 = X1 + side;
// Check if other two point are present or not
if (points.find({ X2, Y1 }) != points.end()
and points.find({ X2, Y2 }) != points.end())
count++;
}
}
}
return count;
}
// Driver Code
int main()
{
int X[] = { 0, 2, 0, 2 }, Y[] = { 0, 2, 2, 0 };
int N = sizeof(X) / sizeof(X[0]);
cout << countSquares(X, Y, N);
return 0;
}
Python3
# Python3 implementation of the approach
# Function that returns the count of
# squares parallel to both X and Y-axis
# from a given set of points
def countSquares(X, Y, N) :
# Initialize result
count = 0;
# Initialize a set to store points
points = [];
# Initialize a map to store the
# points in the same vertical line
vertical = dict.fromkeys(X, None);
# Store the points in a set
for i in range(N) :
points.append((X[i], Y[i]));
# Store the points in the same vertical line
# i.e. with same X co-ordinates
for i in range(N) :
if vertical[X[i]] is None :
vertical[X[i]] = [Y[i]];
else :
vertical[X[i]].append(Y[i]);
# Check for every two points
# in the same vertical line
for line in vertical :
X1 = line;
yList = vertical[line];
for i in range(len(yList)) :
Y1 = yList[i];
for j in range(i + 1, len(yList)) :
Y2 = yList[j];
side = abs(Y1 - Y2);
X2 = X1 + side;
# Check if other two point are present or not
if ( X2, Y1 ) in points and ( X2, Y2 ) in points :
count += 1;
return count;
# Driver Code
if __name__ == "__main__" :
X = [ 0, 2, 0, 2 ]; Y = [ 0, 2, 2, 0 ];
N = len(X);
print(countSquares(X, Y, N));
# This code is contributed by AnkitRai01
1
时间复杂度: O(N 2 )。