C++ Solution O(n^2) 163ms


  • 0
    Z

    The C++ Implementation of solutions by@YUTINGLIU from https://discuss.leetcode.com/topic/66587/clean-java-solution-o-n-2-166ms

    int distance(pair<int,int>& p1, pair<int,int>& p2){
            return (p2.first - p1.first)*(p2.first - p1.first) + (p2.second - p1.second) * (p2.second - p1.second);
    }
    int numberOfBoomerangs(vector<pair<int, int>>& points) {
        unordered_map<int,int> distances;
        int result = 0;
        for(int i = 0; i < points.size(); i++){
            for(int j = 0; j < points.size(); j++){
                if(i == j) continue;
                int d = distance(points[i], points[j]);
                distances[d]++;
            }
            for(pair<int,int> key_value: distances){
                result += key_value.second * (key_value.second - 1);
            }
            distances.clear();
        }
        return result;
    }
    

Log in to reply
 

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