447. Number of Boomerangs

Approach #1 (Brute Force) [Time Limit Exceeded]

Algorithm

Check all the points that satisfied distance( points i , points j ) == distance( points i , points k )

c++

class Solution {
public:
    int distance( pair<int,int> a , pair<int, int > b) {
        //calculate   a*a + b*b    and do not sqrt() because return type is int
        return (a.first-b.first)*(a.first-b.first)+( a.second- b.second )*( a.second- b.second )  ;
    }
    int numberOfBoomerangs(vector<pair<int, int>>& points) {
        int ans =0;
        for(int i=0; i<points.size() ; i++) {
            for(int j=0 ; j<points.size() ; j++ ) {
                for(int k=0 ; k<points.size() ; k++ ) {
                    //make sure satisfied distance( i , j ) == distance( i , k )  and they are distinct
                    if( i!=j && i!=k &&j!= k  && distance( points[i], points[j] ) == distance( points[i],points[k] ) ) {
                        ans++;
                    }
                }
            }
        }
        return ans;
    }
};

Complexity Analysis

  • Time complexity : O(n3) .
    For each point , we try to finds them by looping through the points array which takes O(n) time .Therefore,
    the time complexity is O(n3) .
  • Space complexity : O(1) .
Approach #2(Hash Table) [Accepted]

Algorithm

We optimize the above algorithms with hash table so that we can reduce a layer of loop .
In first loop we new a hash table to calculate every distance between point i and point j .
Then the question will change to a permutation question. For example, { [0,0] , [0,1] , [1,0] , [0,-1] , [-1,0] }
the first point have the same distance with other points . Now we get four points ,for a tuple of points (i, j, k) question,
we should fill these four points to the ( j , k ) – A2n=n!(n2)!

c++

class Solution {
public:
    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 )  ;
    }
    int numberOfBoomerangs(vector<pair<int, int>>& points) {
        int ans =0;
        for(int i=0;i<points.size();i++) {
            unordered_map<int,int> hash;
            for(int j=0;j<points.size() ; j++) {
                if( i == j ) {
                    continue;
                }
                hash[ distance( points[i] , points[j] ) ]++;
            }
            for( auto it=hash.begin() ; it!= hash.end() ;it++ ) {
                ans+= (it->second)*(it->second-1);
            }
        }
        return ans;
    }
};

Complexity Analysis

  • Time complexity : O(n2) .
    Because we reduce a layer of loop so the time complexity is O(n2) .
  • Space complexity : O(k) . k is the max size of hash table .
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值