-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3Sum Closest.py
39 lines (28 loc) · 1.23 KB
/
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
# Link: https://leetcode.com/problems/3sum-closest/submissions/
class Solution:
def threeSumClosest(self, nums: List[int], target: int) -> int:
# If list only has 3 elements, return sum of elements
if len(nums) == 3:
return sum(nums)
# Sort array
nums.sort()
# Get sum of the 1st three elements
closestTarget = sum(nums[:3])
# Iterate list
for i in range(len(nums) - 2): # <= Prevent out-of-bounds error
# Create two pointers at both ends of the list
firstPtr = i + 1
secondPtr = len(nums) - 1
# [-4, -1, 1, 2]
while firstPtr < secondPtr:
# Get current possible sum
currSum = nums[i] + nums[firstPtr] + nums[secondPtr]
# Get closest target
if abs(closestTarget - target) > abs(currSum - target):
closestTarget = currSum
# Update pointes
if currSum < target:
firstPtr += 1
else:
secondPtr -= 1
return closestTarget