subject : Enter an ascending array and a number S, Find two numbers in an array , Yes, their sum is exactly S, If the sum of many pairs of numbers is equal to S, The product of two numbers is the smallest .
Code :
class Solution:
def func(self , nums , s):
if len(nums) <= 1:
return []
lo , hi = 0 , len(nums) - 1
while lo < hi:
if nums[lo] + nums[hi] < s:
lo += 1
if nums[lo] + nums[hi] > s:
hi -= 1
return [nums[lo] , nums[hi]]
return []
The returned is the combination with the smallest product .