leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0447.cpp (697B)


      1 class Solution {
      2     static int distance(const vector<int> &a, const vector<int> &b) {
      3         return pow(b[0] - a[0], 2) + pow(b[1] - a[1], 2);
      4     }
      5 
      6   public:
      7     int numberOfBoomerangs(const vector<vector<int>> &points) const {
      8         const int n = size(points);
      9         int res = 0;
     10 
     11         for (int i = 0; i < n; i++) {
     12             unordered_map<int, int> um;
     13 
     14             for (int j = 0; j < n; j++) {
     15                 if (i == j) continue;
     16                 um[distance(points[i], points[j])]++;
     17             }
     18 
     19             for (const auto [_, cnt] : um) {
     20                 if (cnt == 1) continue;
     21                 res += cnt * (cnt - 1);
     22             }
     23         }
     24 
     25         return res;
     26     }
     27 };