Skip to content

Commit d0a6ecb

Browse files
authored
Merge pull request #51 from tiationg-kho/update
update
2 parents 789fb11 + 194694b commit d0a6ecb

File tree

3 files changed

+239
-507
lines changed

3 files changed

+239
-507
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ Feel free to raise issues or post in discussions.
171171
| 149 | | [646. Maximum Length of Pair Chain](https://leetcode.com/problems/maximum-length-of-pair-chain/) | [E]heap | greedily schedule tasks (start/end/val) | [Python]([E]heap/[E]heap/646-maximum-length-of-pair-chain.py) |
172172
| 150 | | [2008. Maximum Earnings From Taxi](https://leetcode.com/problems/maximum-earnings-from-taxi/) | [E]heap | greedily schedule tasks (start/end/val) | [Python]([E]heap/[E]heap/2008-maximum-earnings-from-taxi.py) |
173173
| 151 | | [2054. Two Best Non-Overlapping Events](https://leetcode.com/problems/two-best-non-overlapping-events/) | [E]heap | greedily schedule tasks (start/end/val) | [Python]([E]heap/[E]heap/2054-two-best-non-overlapping-events.py) |
174-
| 152 | O | [973. K Closest Points to Origin](https://leetcode.com/problems/k-closest-points-to-origin/) | [E]heap | top k problem (based on heap) | [Python]([E]heap/[E]heap/973-k-closest-points-to-origin.py) |
174+
| 152 | O | [973. K Closest Points to Origin](https://leetcode.com/problems/k-closest-points-to-origin/) | [E]heap | top k problem (based on heap) | [Python]([E]heap/[E]heap/973-k-closest-points-to-origin.py) [Java]([E]heap/[E]heap/973-k-closest-points-to-origin.java) |
175175
| 153 | O | [692. Top K Frequent Words](https://leetcode.com/problems/top-k-frequent-words/) | [E]heap | top k problem (based on heap) | [Python]([E]heap/[E]heap/692-top-k-frequent-words.py) |
176176
| 154 | | [264. Ugly Number II](https://leetcode.com/problems/ugly-number-ii/) | [E]heap | k way merge problem | [Python]([E]heap/[E]heap/264-ugly-number-ii.py) |
177177
| 155 | | [313. Super Ugly Number](https://leetcode.com/problems/super-ugly-number/) | [E]heap | k way merge problem | [Python]([E]heap/[E]heap/313-super-ugly-number.py) |
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public int[][] kClosest(int[][] points, int k) {
3+
PriorityQueue<int[]> heap = new PriorityQueue<>((a, b) -> Integer.compare(b[0], a[0]));
4+
int[][] res = new int[k][2];
5+
for (int[] p: points) {
6+
heap.offer(new int[]{p[0] * p[0] + p[1] * p[1], p[0], p[1]});
7+
if (heap.size() > k) {
8+
heap.poll();
9+
}
10+
}
11+
while (!heap.isEmpty()) {
12+
int idx = heap.size() - 1;
13+
int[] p = heap.poll();
14+
res[idx][0] = p[1];
15+
res[idx][1] = p[2];
16+
}
17+
return res;
18+
}
19+
}
20+
21+
// time O(nlogk)
22+
// space O(k)
23+
// using heap and top k problem (based on heap) and max heap

0 commit comments

Comments
 (0)