1
# Runtime: 2003 ms (Top 6.82%) | Memory: 20.5 MB (Top 21.33%)5
return ele[0] ** 2 + ele[1] ** 28
def partition(arr, start, end):9
# We must take a pivot element randomly to make Quick select work faster10
rdIdx = randint(start, end)11
arr[rdIdx], arr[end] = arr[end], arr[rdIdx]14
pivot_dis = cal(pivot)15
for j in range(start, end):16
if cal(arr[j]) <= pivot_dis:18
arr[j], arr[i] = arr[i], arr[j]20
arr[i + 1], arr[end] = arr[end], arr[i + 1]24
def qSelect(arr, kth, start, end):26
pv = partition(arr, start, end)27
# _______________________29
# ------------------------31
# after partition function call, pv is the index that sacrify:32
# all elements in Left will smaller than ele33
# all elements in Right side will greater than ele37
return qSelect(arr, kth, start, pv - 1)39
return qSelect(arr, kth, pv + 1, end)42
# Space O (logn) because of using recursion43
# Time: Average case: O(N)46
def kClosest(self, points, k):49
points, k - 1, 0, len(points) - 150
) # kth smallest number will be at (k-1) index in sorted array