C++ Short, O(N^2) Time O(N) Space, 173ms Beats 90%, Using HashMap


  • 0
    M
    class Solution {
    public:
        inline int sqr(int x) { return x * x; }
        inline int d(pair<int, int> const & p1, pair<int, int> const & p2) {
            return sqr(p1.first - p2.first) + sqr(p1.second - p2.second);
        }
        int numberOfBoomerangs(vector<pair<int, int>>& ps) {
            int result = 0;
            unordered_map<int, int> cnts(ps.size());
            for (auto& i: ps) {
                cnts.clear();
                for (auto& j: ps) ++cnts[d(i, j)];
                for (auto& c: cnts) result += c.second * (c.second - 1);
            }
            return result;
        }
    };
    

Log in to reply
 

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