-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0016.py
66 lines (58 loc) · 2.24 KB
/
0016.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
64
65
66
# Source: https://leetcode.com/problems/3sum-closest
# Title: 3Sum Closest
# Difficulty: Medium
# Author: Mu Yang <http://muyang.pro>
################################################################################################################################
# Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
#
# Example:
#
# Given array nums = [-1, 2, 1, -4], and target = 1.
#
# The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
#
################################################################################################################################
class Solution:
def threeSumClosest(self, nums: List[int], target: int) -> int:
nums.sort()
ans = None
diff = float('Inf')
n = len(nums)
i = 0
while i < n:
ai = nums[i]
if ai > 0 and ai > target + diff: # break when ai too large
break
j = i+1
k = n-1
while j < k:
aj = nums[j]
ak = nums[k]
aijk = ai + aj + ak
if aijk == target:
return target
if aijk > target + diff:
k -= 1
while j < k and nums[k] == ak: # skip duplicate
k -= 1
elif aijk < target - diff:
j += 1
while j < k and nums[j] == aj: # skip duplicate
j += 1
else:
adiff = abs(aijk - target)
if adiff < diff:
diff = adiff
ans = aijk
if aijk > target:
k -= 1
while j < k and nums[k] == ak: # skip duplicate
k -= 1
else:
j += 1
while j < k and nums[j] == aj: # skip duplicate
j += 1
i += 1
while i < n and nums[i] == ai: # skip duplicate
i += 1
return ans