[
2sum
sort
two pointers
]
BinarySearch 0700 Lowest Sum of Pair Larger than Target
Problem statement
https://binarysearch.com/problems/Lowest-Sum-of-Pair-Larger-than-Target/
Solution
Sort numbers and then use classical 2sum problem.
Complexity
It is O(n log n)
for time and O(n)
for space.
Code
class Solution:
def solve(self, nums, target):
nums.sort()
n, ans = len(nums), float("inf")
beg, end = 0, n - 1
while beg < end:
sm = nums[beg] + nums[end]
if sm <= target:
beg += 1
elif sm > target:
ans = min(ans, sm)
end -= 1
return ans