-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAmazonThreeSumClosest.py
40 lines (34 loc) · 1.49 KB
/
AmazonThreeSumClosest.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
class Solution:
def threeSumClosest(self, nums: List[int], target: int) -> int:
nums.sort() # for two pointers sorted array needed
diff = float('inf') # intialize diff to max value
for i in range(len(nums)-2): # run loop till len(nums) or till len(nums)-2
lo = i + 1
hi = len(nums) - 1
while lo < hi :
sum = nums[i] + nums[lo] + nums[hi]
if abs(target - sum) < abs(diff): # checking if abs of target- sum is less than diff
diff = target - sum # assign to diff
elif sum < target:
lo += 1
else :
hi -= 1
if diff == 0: # if diff is zero break
break
return target - diff # as sum = target - diff
"""
Remaining : Binary Search Approach, threeSumSmaller
Above code Algorithm :
Initialize the minimum difference diff with a large value.
Sort the input array nums.
Iterate through the array:
For the current position i, set lo to i + 1, and hi to the last index.
While the lo pointer is smaller than hi:
Set sum to nums[i] + nums[lo] + nums[hi].
If the absolute difference between sum and target is smaller than the absolute value of diff:
Set diff to target - sum.
If sum is less than target, increment lo.
Else, decrement hi.
If diff is zero, break from the loop.
Return the value of the closest triplet, which is target - diff.
"""