164ms Does the OP show an incorrect ans on repeating inputs?


  • 0
    A

    The sample input I gave is : [[0,0],[1,0],[2,0],[1,0]]
    The output provided by the system was 12. Manually I could only calculate 8. Also, my algo returned 8 as well.

    Am I missing something or is the answer given by the system wrong?

    Here's my code

    public int numberOfBoomerangs(int[][] points) {
        int count = 0;
        for(int i=0;i<points.length;i++){
            HashMap<Double,Integer> s= new HashMap<>();
            for(int j = 0;j<points.length;j++){
                if(i == j) continue;
                double d = getDistance(points[i],points[j]);
                if(s.containsKey(d)){
                    int get = s.get(d);
                    count = count + 2*get;
                    s.put(d,get+1);
                }
                else
                    s.put(d,1);
            }
        }
        return count;
    }
    
    public double getDistance(int[] a, int[] b){
        int x = a[0] - b[0];
        x = x*x;
        int y = a[1] - b[1];
        y = y*y;
        return Math.sqrt(x+y);
    }

  • 1

    @ashwin.neelakantan

    Given n points in the plane that are all pairwise distinct

    Therefore, your input is invalid because the point [1,0] is repeated once.


  • 0
    A

    @1337c0d3r Ohhh...I missed that. I'm still intrigued by the approach used by the system to solve the problem as its output is different form mine.


Log in to reply
 

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