Skip to content

Commit 49fe32b

Browse files
committed
Runtime: 2003 ms (Top 6.82%) | Memory: 20.5 MB (Top 21.33%)
1 parent 1aeee80 commit 49fe32b

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# Runtime: 2003 ms (Top 6.82%) | Memory: 20.5 MB (Top 21.33%)
12

23
def cal(ele):
34
return ele[0]**2 +ele[1]**2
@@ -13,30 +14,30 @@ def partition(arr,start,end):
1314
if cal(arr[j]) <= pivot_dis:
1415
i+=1
1516
arr[j],arr[i] = arr[i],arr[j]
16-
17+
1718
arr[i+1],arr[end] = arr[end],arr[i+1]
1819
return i+1
1920
def qSelect(arr,kth,start,end):
2021
if start < end:
2122
pv= partition(arr,start,end)
22-
# _______________________
23-
# | Left |ele| Right|
24-
# ------------------------
25-
# pv
23+
# _______________________
24+
# | Left |ele| Right|
25+
# ------------------------
26+
# pv
2627
# after partition function call, pv is the index that sacrify:
2728
# all elements in Left will smaller than ele
28-
# all elements in Right side will greater than ele
29+
# all elements in Right side will greater than ele
2930
if pv == kth:#
3031
return
3132
if kth < pv:
3233
return qSelect(arr,kth,start,pv-1)
3334
else:
3435
return qSelect(arr,kth,pv+1,end)
35-
# Space O (logn) because of using recursion
36+
# Space O (logn) because of using recursion
3637
# Time: Average case: O(N)
37-
# Worst case: O(N**2)
38+
# Worst case: O(N**2)
3839
class Solution:
3940
def kClosest(self, points, k):
4041
# print(points)
4142
qSelect(points,k-1,0,len(points)-1)# kth smallest number will be at (k-1) index in sorted array
42-
return points[:k]
43+
return points[:k]

0 commit comments

Comments
 (0)