-
Notifications
You must be signed in to change notification settings - Fork 0
/
16 3sum closest
24 lines (24 loc) · 1.41 KB
/
16 3sum closest
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
def threeSumClosest(self, nums, target):
nums.sort()
global_closest_diff,global_sum = math.inf,0
for first in range(len(nums)-2):
second,third = first + 1,len(nums)-1
if first - 1 >= 0 and nums[first] == nums[first-1]:
continue
while second < third:
threesum = nums[first] + nums[second] + nums[third]
if threesum >= target:
while second < third and nums[first]+nums[second]+nums[third] >= target: #advance third to find turning point !
if threesum == target:
return target
third -= 1
if abs(nums[first]+nums[second]+nums[third+1]-target) < global_closest_diff:
global_sum = nums[first]+nums[second]+nums[third+1]
global_closest_diff = abs(nums[first]+nums[second]+nums[third+1]-target)
else:
while second < third and nums[first]+nums[second]+nums[third] < target: #advance second to find turning point !
second += 1
if abs(nums[first]+nums[second-1]+nums[third]-target) < global_closest_diff:
global_sum = nums[first]+nums[second-1]+nums[third]
global_closest_diff = abs(nums[first]+nums[second-1]+nums[third]-target)
return global_sum