Find K Closest Elements LeetCode solution in Python
1class Solution:2def findClosestElements(self, arr: List[int], k: int, x: int) -> List[int]:34def sorted_distance(value, static_input=x):5return abs(value - static_input)67distances = []8result = []9heapq.heapify(distances)1011for l, v in enumerate(arr):12distances.append((l, sorted_distance(value=v)))1314for i in heapq.nsmallest(k, distances, key=lambda x: x[1]):15result.append(arr[i[0]])1617result.sort()18return result