-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path0658-find-k-closest-elements.py
More file actions
59 lines (51 loc) · 1.6 KB
/
0658-find-k-closest-elements.py
File metadata and controls
59 lines (51 loc) · 1.6 KB
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
# time complexity: O(logn)
# space complexity: O(1)
from typing import List
class Solution:
def findClosestElements(self, nums: List[int], k: int, target: int) -> List[int]:
def binarySearch(nums: List[int], target: int) -> int:
left = 0
right = len(nums) - 1
while left <= right:
mid = left + (right - left) // 2
if nums[mid] == target:
return mid
if nums[mid] < target:
left = mid + 1
else:
right = mid - 1
return left
if len(nums) == k:
return nums
if target <= nums[0]:
return nums[:k]
if nums[-1] <= target:
return nums[len(nums) - k: len(nums)]
firstCloset = binarySearch(nums, target)
leftCloset = firstCloset - 1
rightCloset = leftCloset + 1
while (rightCloset - leftCloset - 1) < k:
if leftCloset == -1:
rightCloset += 1
continue
if rightCloset == len(nums) or abs(nums[leftCloset] - target) <= abs(nums[rightCloset] - target):
leftCloset -= 1
else:
rightCloset += 1
return nums[leftCloset + 1:rightCloset]
arr = [1, 2, 3, 4, 5]
k = 3
x = 4
print(Solution().findClosestElements(arr, k, x))
arr = [1, 2, 3, 4, 5]
k = 4
x = 3
print(Solution().findClosestElements(arr, k, x))
arr = [1, 1, 2, 3, 4, 5]
k = 4
x = -1
print(Solution().findClosestElements(arr, k, x))
arr = [1, 2]
k = 1
x = 1
print(Solution().findClosestElements(arr, k, x))