-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path658.py
63 lines (58 loc) · 2.12 KB
/
658.py
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
60
61
62
63
__________________________________________________________________________________________________
sample 320 ms submission
class Solution:
def findClosestElements(self, arr: List[int], k: int, x: int) -> List[int]:
# binary search
left = 0
right = len(arr) - k
while left < right:
mid = left + (right - left) // 2
if x - arr[mid] > arr[mid + k] - x:
left = mid + 1
else:
right = mid
return arr[left : left + k]
__________________________________________________________________________________________________
sample 14048 kb submission
class Solution:
def findClosestElements(self, arr: List[int], k: int, x: int) -> List[int]:
lb = 0
ub = len(arr) - 1
arrUb = ub
found = False
while lb <= ub:
mid = int((lb + ub) / 2)
if arr[mid] == x:
found = True
break
if arr[mid] < x:
lb = mid + 1
else:
ub = mid - 1
uperList = []
lowerList = []
itemsAdded = 0
lowerArrIndex = ub
uperArrIndex = lb
if (found):
uperList.append(arr[mid])
lowerArrIndex = mid - 1
uperArrIndex = mid + 1
itemsAdded += 1
while itemsAdded < k:
if lowerArrIndex < 0:
uperList.append(arr[uperArrIndex])
uperArrIndex += 1
elif uperArrIndex > arrUb:
lowerList.append(arr[lowerArrIndex])
lowerArrIndex -= 1
elif x - arr[lowerArrIndex] <= arr[uperArrIndex] - x:
lowerList.append(arr[lowerArrIndex])
lowerArrIndex -= 1
elif x - arr[lowerArrIndex] > arr[uperArrIndex] - x:
uperList.append(arr[uperArrIndex])
uperArrIndex += 1
itemsAdded += 1
lowerList.reverse()
return lowerList + uperList
__________________________________________________________________________________________________