-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathk-closest-points-to-origin.py
71 lines (64 loc) · 2.46 KB
/
k-closest-points-to-origin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# 973. K Closest Points to Origin
# 🟠 Medium
#
# https://leetcode.com/problems/k-closest-points-to-origin/
#
# Tags: Array - Math - Divide and Conquer - Geometry - Sorting -
# Heap (Priority Queue) - Quickselect
import timeit
from heapq import heappush, heappushpop
from typing import List
# Use a min heap to store the k elements closest to the origin. We can
# push or push and pop an element to the heap in O(log(k)).
#
# Time complexity: O(n*log(k)) - Pushing n elements into the heap at
# log(k) cost each, we could improve this checking if k is greater than
# n/2, in that case we could heapify the input in O(n) then pop n-k
# elements in O(log(k)), that would improve the complexity to
# O(min(k, n-k)*log(k))
# Space complexity: O(k) - The size of the heap.
#
# Runtime: 1756 ms, faster than 31.3%
# Memory Usage: 20.4 MB, less than 61.68%
class Heap:
def kClosest(self, points: List[List[int]], k: int) -> List[List[int]]:
# Use a heap to store the k closest points to the origin.
heap = []
# Iterate over the input points.
for x, y in points:
# Calculate the square of the distance to the origin
dis = x * x + y * y
# Push or push-and-pop to the heap preserving size k.
if len(heap) < k:
heappush(heap, (-dis, x, y))
else:
heappushpop(heap, (-dis, x, y))
# Cast the heap elements to a list keeping only the second and
# third values in each. Order is not important.
return [[x, y] for d, x, y in list(heap)]
def test():
executors = [Heap]
tests = [
[[[1, 3], [-2, 2]], 1, [[-2, 2]]],
[[[3, 3], [5, -1], [-2, 4]], 2, [[3, 3], [-2, 4]]],
]
for executor in executors:
start = timeit.default_timer()
for _ in range(10000):
for col, t in enumerate(tests):
sol = executor()
result = sol.kClosest(t[0], t[1])
exp = t[2]
# The order is not important
result.sort()
exp.sort()
assert result == exp, (
f"\033[93m» {result} <> {exp}\033[91m for"
+ f" test {col} using \033[1m{executor.__name__}"
)
stop = timeit.default_timer()
used = str(round(stop - start, 5))
cols = "{0:20}{1:10}{2:10}"
res = cols.format(executor.__name__, used, "seconds")
print(f"\033[92m» {res}\033[0m")
test()