-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathclosest-number-in-sorted-array.py
More file actions
32 lines (31 loc) · 995 Bytes
/
closest-number-in-sorted-array.py
File metadata and controls
32 lines (31 loc) · 995 Bytes
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
class Solution:
"""
@param A: an integer array sorted in ascending order
@param target: An integer
@return: an integer
"""
def closestNumber(self, A, target):
# write your code here
x, y, z = -1, -1, -1
i, j = 0, len(A) - 1
while i <= j:
mid = int((i + j) / 2)
if A[mid] < target:
if (mid < (len(A) - 1)) and (A[mid + 1] >= target):
y = mid
i = mid + 1
elif A[mid] > target:
if (0 < mid) and (A[mid - 1] <= target):
z = mid
j = mid - 1
else:
x = mid
break
if x != -1:
return x
if (y == -1) and (A[-1] < target):
return len(A) - 1
if (z == -1) and (A[0] > target):
return 0
return y if (target - A[y]) <= (A[z] - target) else z
# easy: https://www.lintcode.com/problem/459