-
Notifications
You must be signed in to change notification settings - Fork 17
/
16-3sum-closest.py
39 lines (34 loc) · 1.36 KB
/
16-3sum-closest.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
class Solution:
def threeSumClosest(self, nums: List[int], target: int) -> int:
nums.sort()
res_diff = float('inf')
res = None
for i in range(len(nums) - 2):
left, right = i + 1, len(nums) - 1
small_three = nums[i] + nums[left] + nums[left + 1]
if small_three > target:
if abs(small_three - target) < res_diff:
res_diff = abs(small_three - target)
res = small_three
break
large_three = nums[i] + nums[right - 1] + nums[right]
if large_three < target:
if abs(large_three - target) < res_diff:
res_diff = abs(large_three - target)
res = large_three
continue
while left < right:
cur = nums[i] + nums[left] + nums[right]
if abs(cur - target) < res_diff:
res_diff = abs(cur - target)
res = cur
if cur == target:
return target
elif cur > target:
right -= 1
else:
left += 1
return res
# time O(n**2)
# space O(1), not count built in sort's cost
# using array and two pointers opposite direction and shrink type and sort and prune