---
# System prepended metadata

title: 973. K Closest Points to Origin
tags: [Heap]

---

---
title: 973. K Closest Points to Origin
tags: Heap
description: share source code.
---

# 973. K Closest Points to Origin

```java

// used priorityQueue to find top K
class Solution {
    public int[][] kClosest(int[][] points, int k) {
        PriorityQueue<int []> q = new PriorityQueue<>( (a, b) -> {
            return (a[0]*a[0] + a[1]*a[1]) - (b[0]*b[0] +b[1]*b[1]) ;
        });
        
        for(int [] p : points){
            q.offer(p);
        }
        
        List<int []> ret = new ArrayList<>(); 
        
        int i = 0;
        while(i++ < k){
            ret.add(q.poll());
        }
        
        return ret.toArray(new int [ret.size()][]);
    }
}
```

