📋

Array Code

Three Sum 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
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
"""
Three sum 

find 3 integers summing closest to a target, 

return their sum 

Input: nums = [-1,2,1,-4], target = 1
Output: 2
Explanation: The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
"""

def threeSumClosest(self, nums: List[int], target: int) -> int:
    """
    sort the list 

    for each num:
        start left and right pointers at 0 and n-1 

        if left + right + num is target return 
        if the sum is closer, update the result 

        if less then sum, move left, else move right 
    """
    
    n = len(nums)
    
    nums.sort()

    res = None
    
    resdif = float("inf")
    

    for i,num in enumerate(nums):

        
        # skip dups 
        if i>0 and num==nums[i-1]: 
            continue

        l = i+1
        r = n-1
        
        while l<r:

            total = num + nums[l] + nums[r] 

            if total == target:
                return target 
            
            # update diff 
            diff = abs(total-target)
            if diff < resdif:
                res = total
                resdif = diff 
                
            
            if total < target:
                l += 1
            else:
                r -= 1

    return res