我正在解决 GeeksforGeeks 上的一个问题,该问题涉及使用三个不同的数组元素作为三角形的边从大小为n的未排序数组arr[]中找到可能的三角形总数。这是上述问题的第一个实现。...
int findNumberOfTriangles(int arr[], int n)
{
// code here
sort(arr,arr+n);
int i,j,k,count=0;
for(i=0;i<n-2;i++)//first side of triangle
{
k=i+2; // third side of triangle , ie. rightmost element initialized
for(j=i+1;j<n;j++) //second side of triangle
{
// search for all elements less than sum of the
// first and second side of the triangle
while(k<n)
{
if(arr[i]+arr[j]>arr[k])
k++;
}
count=count+ k-j-1; //adds no of triangle possible for each pair of sides
}
}
return count;
}
...上述实现是正确的,应该具有O(N^2)时间复杂度。对于上面的代码,我从 GeeksforGeeks 平台获得了一个TLE(Time Limit exceeded) ,预期时间限制=1.02 sec。
现在将以下实现与上述实现进行比较。
int findNumberOfTriangles(int arr[], int n)
{
// code here
sort(arr,arr+n);
int i,j,k,count=0;
for(i=0;i<n-2;i++)//first side of triangle
{
k=i+2; // third side of triangle , ie. rightmost element initialized
for(j=i+1;j<n;j++) //second side of triangle
{
// search for all elements less than sum of the
// first and second side of the triangle
while(k<n && arr[i]+arr[j]>arr[k])
k++;
count=count+ k-j-1; //adds no of triangle possible for each pair of sides
}
}
return count;
}
实现仅在第二个for 循环内的while 循环中有所不同。我通过使用逻辑 AND 操作将if 条件从while 主体内部移动到条件声明部分。时间复杂度与第一个实现相同:O(N^2)。
但是,这个实现被GeeksforGeeks接受,时间为 = (0/1.0)。
有人可以告诉我为什么两段等效代码之间存在如此大的性能差异。这是由于 GeeksforGeeks 平台还是由于 C++ 语言的特性。GeeksforGeeks 使用g++5.4编译器
链接:https ://practice.geeksforgeeks.org/problems/count-possible-triangles-1587115620/1