Skip to content

Commit 55c4c62

Browse files
committed
added K Closest Points to Origin (medium)
1 parent 87a5242 commit 55c4c62

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# K Closest Points to Origin
2+
3+
[Leetcode Link](https://leetcode.com/problems/k-closest-points-to-origin/)
4+
5+
## Problem:
6+
7+
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
8+
9+
(Here, the distance between two points on a plane is the Euclidean distance.)
10+
11+
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
12+
13+
## Example:
14+
15+
```
16+
Input: points = [[1,3],[-2,2]], K = 1
17+
Output: [[-2,2]]
18+
Explanation:
19+
The distance between (1, 3) and the origin is sqrt(10).
20+
The distance between (-2, 2) and the origin is sqrt(8).
21+
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
22+
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
23+
```
24+
25+
```
26+
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
27+
Output: [[3,3],[-2,4]]
28+
(The answer [[-2,4],[3,3]] would also be accepted.)
29+
```
30+
31+
## Note:
32+
33+
- 1 <= K <= points.length <= 10000
34+
- -10000 < points[i][0] < 10000
35+
- -10000 < points[i][1] < 10000
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
from typing import List
2+
3+
4+
class MaxHeap():
5+
def __init__(self):
6+
self.heap = []
7+
8+
def insert(self, val):
9+
self.heap.append(val)
10+
self.heapify()
11+
12+
def getMax(self):
13+
return self.heap[0]
14+
15+
def popMax(self):
16+
maxVal = self.heap.pop(0)
17+
self.heapify()
18+
19+
def size(self):
20+
return len(self.heap)
21+
22+
def heapify(self):
23+
for i in range(len(self.heap)-1, 0, -1):
24+
parentIndex = (i-1) // 2
25+
if self.heap[i][0] > self.heap[parentIndex][0]:
26+
self.heap[i], self.heap[parentIndex] = self.heap[parentIndex], self.heap[i]
27+
28+
def __str__(self):
29+
return str(self.heap)
30+
31+
def __iter__(self):
32+
return (x for x in self.heap)
33+
34+
35+
class Solution:
36+
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
37+
closest = list(list())
38+
# find Euclidean distance and put them in key value pair
39+
distances = []
40+
for point in points:
41+
squareSum = point[0]**2 + point[1]**2
42+
distances.append((squareSum, point))
43+
print(distances)
44+
# use max heap of size K to only hold K closest pairs
45+
maxHeap = MaxHeap()
46+
for distance, point in distances:
47+
# print(distance, point)
48+
# if maxheap has space, then insert
49+
if maxHeap.size() < K:
50+
maxHeap.insert((distance, point))
51+
else:
52+
if distance < maxHeap.getMax()[0]:
53+
maxHeap.popMax()
54+
maxHeap.insert((distance, point))
55+
# return all K pairs in max heap
56+
print([pair[1] for pair in maxHeap])
57+
closest.extend([pair[1] for pair in maxHeap])
58+
return closest
59+
60+
61+
sol = Solution()
62+
points = [[3, 3], [5, -1], [-2, 4], [3, 4], [1, 5], [-2, -5], [0, 8]]
63+
K = 4
64+
print(sol.kClosest(points, K))

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ Languages used: Java and Python
8484
- [Perfect Squares](Medium/PerfectSquares)
8585
- [Score After Flipping Matrix](Medium/ScoreAfterFlippingMatrix)
8686
- [Construct Binary Tree from Preorder and Inorder Traversal](Medium/ConstructBinaryTree)
87+
- [K Closest Points to Origin](Medium/KClosestPointsToOrigin)
8788
- Hard
8889
- [Maximum Score Words Formed by Letters](Hard/MaximumScoreWords)
8990
- [Reducing Dishes](Hard/ReducingDishes)

0 commit comments

Comments
 (0)