Skip to content

Commit de46123

Browse files
varunu28fishercoder1534
authored andcommitted
Added _973.java (fishercoder1534#32)
Added _973.java
1 parent 9de220b commit de46123

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Your ideas/fixes/algorithms are more than welcome!
2929

3030
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
3131
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
32+
|973|[K Closest Points to Origin](https://leetcode.com/problems/k-closest-points-to-origin/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_973.java) | O(nlogn) | O(K) | |Easy| Math Sort
3233
|970|[Powerful Integers](https://leetcode.com/problems/powerful-integers/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_970.java) | O(?) | O(1) | |Easy| Math
3334
|966|[Vowel Spellchecker](https://leetcode.com/problems/vowel-spellchecker/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_966.java) | O(hlogn) | O(n) | |Medium| Hash Table, String
3435
|965|[Univalued Binary Tree](https://leetcode.com/problems/univalued-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_965.java) | O(n) | O(h) | |Easy| DFS, recursion|
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.Comparator;
4+
import java.util.PriorityQueue;
5+
6+
public class _973 {
7+
8+
public static class Solution1 {
9+
public int[][] kClosest(int[][] points, int K) {
10+
int[][] ans = new int[K][2];
11+
12+
PriorityQueue<int[]> pq = new PriorityQueue<>(new Comparator<int[]>() {
13+
@Override
14+
public int compare(int[] o1, int[] o2) {
15+
double dist1 = getDistance(o1);
16+
double dist2 = getDistance(o2);
17+
18+
if (dist1 > dist2) {
19+
return 1;
20+
} else if (dist1 < dist2) {
21+
return -1;
22+
} else {
23+
return 0;
24+
}
25+
}
26+
});
27+
28+
for (int[] point : points) {
29+
pq.add(point);
30+
}
31+
32+
for (int i=0; i<K; i++) {
33+
ans[i] = pq.poll();
34+
}
35+
36+
return ans;
37+
}
38+
39+
private double getDistance(int[] point) {
40+
return Math.sqrt(Math.pow(point[0], 2) + Math.pow(point[1], 2));
41+
}
42+
}
43+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._973;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertArrayEquals;
8+
9+
public class _973Test {
10+
private static _973.Solution1 test;
11+
12+
@BeforeClass
13+
public static void setUp() {
14+
test = new _973.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertArrayEquals(new int[][]{{-2, 2}}, test.kClosest(new int[][]{{1, 3}, {-2, 2}}, 1));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertArrayEquals(new int[][]{{3, 3},{-2, 4}}, test.kClosest(new int[][]{{3, 3},{5, -1},{-2, 4}}, 2));
25+
}
26+
}

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