Skip to content

Commit 8dbd0ab

Browse files
refactor 447
1 parent f760adb commit 8dbd0ab

File tree

2 files changed

+89
-49
lines changed

2 files changed

+89
-49
lines changed

src/main/java/com/fishercoder/solutions/_447.java

Lines changed: 45 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@
33
import java.util.HashMap;
44
import java.util.Map;
55

6-
/**Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of points (i, j, k) such that the distance between i and j equals the distance between i and k (the order of the tuple matters).
7-
8-
Find the number of boomerangs. You may assume that n will be at most 500 and coordinates of points are all in the range [-10000, 10000] (inclusive).
6+
/**
7+
* 447. Number of Boomerangs
8+
*
9+
* Given n points in the plane that are all pairwise distinct,
10+
* a "boomerang" is a tuple of points (i, j, k) such that the distance
11+
* between i and j equals the distance between i and k (the order of the tuple matters).
12+
*
13+
* Find the number of boomerangs.
14+
* You may assume that n will be at most 500 and coordinates of points are all in the range [-10000, 10000] (inclusive).
915
1016
Example:
1117
Input:
@@ -17,57 +23,47 @@
1723
Explanation:
1824
The two boomerangs are [[1,0],[0,0],[2,0]] and [[1,0],[2,0],[0,0]]*/
1925
public class _447 {
20-
/**Looked at these two posts: https://discuss.leetcode.com/topic/66587/clean-java-solution-o-n-2-166ms and
21-
* https://discuss.leetcode.com/topic/66521/share-my-straightforward-solution-with-hashmap-o-n-2, basically,
22-
* have a HashMap, key is the distance, value is the number of points that are this distance apart to this point.
23-
* Note: we clear up this map every time after we traverse one point with the rest of the other points.
24-
*
25-
* Time complexity: O(n^2)
26-
* Space complexity: O(n)*/
2726

28-
public int numberOfBoomerangs(int[][] points) {
29-
int result = 0;
30-
if (points == null || points.length == 0 || points[0].length == 0) {
31-
return result;
32-
}
33-
int totalPts = points.length;
34-
Map<Long, Integer> map = new HashMap();
35-
for (int i = 0; i < totalPts; i++) {
36-
for (int j = 0; j < totalPts; j++) {
37-
if (i == j) {
38-
continue;
39-
}
40-
long d = calcDistance(points[i], points[j]);
41-
map.put(d, map.getOrDefault(d, 0) + 1);
27+
public static class Solution1 {
28+
/**
29+
* Looked at these two posts: https://discuss.leetcode.com/topic/66587/clean-java-solution-o-n-2-166ms and
30+
* https://discuss.leetcode.com/topic/66521/share-my-straightforward-solution-with-hashmap-o-n-2, basically,
31+
* have a HashMap, key is the distance, value is the number of points that are this distance apart to this point.
32+
* Note: we clear up this map every time after we traverse one point with the rest of the other points.
33+
* <p>
34+
* Time complexity: O(n^2)
35+
* Space complexity: O(n)
36+
*/
37+
38+
public int numberOfBoomerangs(int[][] points) {
39+
int result = 0;
40+
if (points == null || points.length == 0 || points[0].length == 0) {
41+
return result;
4242
}
43+
int totalPts = points.length;
44+
Map<Long, Integer> map = new HashMap();
45+
for (int i = 0; i < totalPts; i++) {
46+
for (int j = 0; j < totalPts; j++) {
47+
if (i == j) {
48+
continue;
49+
}
50+
long d = calcDistance(points[i], points[j]);
51+
map.put(d, map.getOrDefault(d, 0) + 1);
52+
}
4353

44-
for (int val : map.values()) {
45-
result += val * (val - 1);
54+
for (int val : map.values()) {
55+
result += val * (val - 1);
56+
}
57+
map.clear();
4658
}
47-
map.clear();
59+
return result;
4860
}
49-
return result;
50-
}
5161

52-
private long calcDistance(int[] p1, int[] p2) {
53-
long x = p2[0] - p1[0];
54-
long y = p2[1] - p1[1];
55-
return x * x + y * y;
62+
private long calcDistance(int[] p1, int[] p2) {
63+
long x = p2[0] - p1[0];
64+
long y = p2[1] - p1[1];
65+
return x * x + y * y;
66+
}
5667
}
5768

58-
public static void main(String... args) {
59-
_447 test = new _447();
60-
// int[][] points = new int[][]{
61-
// {0,0},
62-
// {1,0},
63-
// {2,0},
64-
// };
65-
66-
// [[3,6],[7,5],[3,5],[6,2],[9,1],[2,7],[0,9],[0,6],[2,6]], should return 10
67-
int[][] points = new int[][] { { 3, 6 }, { 7, 5 }, { 3, 5 }, { 6, 2 }, { 9, 1 }, { 2, 7 },
68-
{ 0, 9 }, { 0, 6 }, { 2, 6 }, };
69-
70-
// [[0,0],[1,0],[-1,0],[0,1],[0,-1]] should return 20
71-
System.out.println(test.numberOfBoomerangs(points));
72-
}
73-
}
69+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._447;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _447Test {
10+
private static _447.Solution1 solution1;
11+
private static int[][] points;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _447.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
points = new int[][]{
21+
{0, 0},
22+
{1, 0},
23+
{2, 0},
24+
};
25+
assertEquals(2, solution1.numberOfBoomerangs(points));
26+
}
27+
28+
@Test
29+
public void test2() {
30+
points = new int[][]{
31+
{3, 6},
32+
{7, 5},
33+
{3, 5},
34+
{6, 2},
35+
{9, 1},
36+
{2, 7},
37+
{0, 9},
38+
{0, 6},
39+
{2, 6}
40+
};
41+
assertEquals(10, solution1.numberOfBoomerangs(points));
42+
}
43+
44+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy