Why TLE?


  • 0
    M

    Here is my code and I think its time complexity is O(n^2). I dont know why TLE..... Please help....THX!

    class Solution {
    public:
        int numberOfBoomerangs(vector<pair<int, int>>& points) {
            multiset<int> temp;
            if(points.size()<3) return 0;
            int count=0;
            multiset<int>::iterator p;
            for(int i=0;i<points.size();i++)
            {
                for(int j=0;j<points.size();j++)
                {
                    temp.insert(distance(points[i],points[j]));
                }
                p=temp.begin();
                while(p!=temp.end())
                {
                    if(temp.count(*p)>1) count+=temp.count(*p)*(temp.count(*p)-1);
                    temp.erase(*p);
                    p=temp.begin();
                }
                temp.clear();
            }
            return count;
        }
        
        int distance(pair<int,int> a,pair<int,int> b)
        {
            return (a.first-b.first)*(a.first-b.first)+(a.second-b.second)*(a.second-b.second);
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.