Skip to content

Commit 08e37c4

Browse files
committed
973_K_Closest_Points_to_Origin
1 parent a9f9814 commit 08e37c4

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ Also, there are open source implementations for basic data structs and algorithm
209209
| 961 | [N-Repeated Element in Size 2N Array](https://leetcode.com/problems/n-repeated-element-in-size-2n-array/submissions/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/961_N-Repeated_Element_in_Size_2N_Array.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/961_N-Repeated_Element_in_Size_2N_Array.java) | Hash and count number, O(n) and O(n) |
210210
| 962 | [Maximum Width Ramp](https://leetcode.com/problems/maximum-width-ramp/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/962_Maximum_Width_Ramp.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/962_Maximum_Width_Ramp.java) | 1. Sort index by value, then transfer problem into finding max gap between index, O(nlogn) and O(1)<br>2. Binary Search for candicates, O(nlogn) and O(n) |
211211
| 977 | [Squares of a Sorted Array](https://leetcode.com/problems/squares-of-a-sorted-array/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/977_Squares_of_a_Sorted_Array.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/977_Squares_of_a_Sorted_Array.java) | 1. Sort, O(nlogn) and O(n)<br>2. Two point, O(n) and O(n) |
212+
| 973 | [K Closest Points to Origin](https://leetcode.com/problems/k-closest-points-to-origin/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/973_K_Closest_Points_to_Origin.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/973_K_Closest_Points_to_Origin.java) | 1. Sort and get 0-K, O(nlogn) and O(1)<br>2. Min Heap, O(nlogk) and O(k) |
212213
| 1064 | [Fixed Point](https://leetcode.com/problems/fixed-point/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/1064_Fixed_Point.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/1064_Fixed_Point.java) | 1. Go through index and value, until find solution encounter index < value, O(n) and O(1)<br>2. Binary search, O(logn) and O(1) |
213214

214215
| # | To Understand |
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Solution {
2+
/* public int[][] kClosest(int[][] points, int K) {
3+
// Sort
4+
int N = points.length;
5+
int[] dists = new int[N];
6+
for (int i = 0; i < N; ++i)
7+
dists[i] = dist(points[i]);
8+
9+
Arrays.sort(dists);
10+
int distK = dists[K-1];
11+
12+
int[][] ans = new int[K][2];
13+
int t = 0;
14+
for (int i = 0; i < N; ++i)
15+
if (dist(points[i]) <= distK)
16+
ans[t++] = points[i];
17+
return ans;
18+
} */
19+
20+
private int dist(int[] point) {
21+
return point[0] * point[0] + point[1] * point[1];
22+
}
23+
24+
public int[][] kClosest(int[][] points, int K) {
25+
// Min Heap with PriorityQueue
26+
PriorityQueue<int[]> pq = new PriorityQueue<int[]>((p1, p2) -> dist(p2) - dist(p1));
27+
for (int[] p : points) {
28+
pq.offer(p);
29+
if (pq.size() > K) {
30+
pq.poll();
31+
}
32+
}
33+
int[][] res = new int[K][2];
34+
while (K > 0) {
35+
res[--K] = pq.poll();
36+
}
37+
return res;
38+
}
39+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution(object):
2+
# def kClosest(self, points, K):
3+
# """
4+
# :type points: List[List[int]]
5+
# :type K: int
6+
# :rtype: List[List[int]]
7+
# """
8+
# # Sort
9+
# return sorted(points, key=lambda x: x[0] ** 2 + x[1] ** 2)[:K]
10+
11+
def kClosest(self, points, K):
12+
# K smallest heaq
13+
return heapq.nsmallest(K, points, key=lambda x: x[0] ** 2 + x[1] ** 2)

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