The k strongest values in an array
November 5, 2022
array-and-hashmapProblem URL: The k strongest values in an array
We will calture the median of the array, and then use two pointers to find the k strongest values.
class Solution:
def getStrongest(self, arr: List[int], k: int) -> List[int]:
arr.sort()
median = arr[(len(arr)-1)//2]
res = []
i, j = 0, len(arr)-1
while len(res) < k:
if abs(arr[i]-median) > abs(arr[j]-median):
res.append(arr[i])
i += 1
else:
res.append(arr[j])
j -= 1
return res
Time Complexity: O(nlogn)
Space Complexity: O(1)