-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy path3sum-closest.py
More file actions
47 lines (33 loc) · 1.32 KB
/
3sum-closest.py
File metadata and controls
47 lines (33 loc) · 1.32 KB
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
class Solution:
def threeSumClosest(self, nums: list[int], target: int) -> int:
nums.sort()
result = nums[0] + nums[1] + nums[2]
for pos in range(len(nums) - 2):
left, right = pos + 1, len(nums) - 1
while left < right:
cur_sum = nums[pos] + nums[left] + nums[right]
if cur_sum < target:
left += 1
elif cur_sum > target:
right -= 1
else:
return cur_sum
result = min(result, cur_sum, key=lambda x: abs(target - x))
return result
class TestSolution:
def setup(self):
self.sol = Solution()
def test_empty(self):
assert self.sol.threeSumClosest([], 0) == 0
assert self.sol.threeSumClosest([1], 0) == 0
assert self.sol.threeSumClosest([1, 2], 0) == 0
def test_custom1(self):
assert self.sol.threeSumClosest([1, 2, 3], 0) == 6
def test_custom2(self):
assert self.sol.threeSumClosest([-1, 2, 1, -4], 1) == 2
def test_custom3(self):
assert self.sol.threeSumClosest([0, 1, 2], 3) == 3
def test_custom4(self):
assert self.sol.threeSumClosest([1, 1, 1, 0], -100) == 2
def test_custom5(self):
assert self.sol.threeSumClosest([0, 2, 1, -3], 1) == 0