-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path__init__.py
More file actions
56 lines (47 loc) · 2.06 KB
/
__init__.py
File metadata and controls
56 lines (47 loc) · 2.06 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
48
49
50
51
52
53
54
55
56
from typing import List
def two_sum_less_than_k(nums: List[int], k: int) -> int:
"""
Finds the maximum sum of two elements in a given list of numbers that is less than k.
Uses binary search to achieve a time complexity of O(n log n) and find the maximum sum of two elements
that is less than k. It takes the nums array and the target value k as input.
Args:
nums (List[int]): A sorted list of integers
k int: The target value to search for
Returns:
The maximum sum of two elements that is less than k
"""
max_sum = -1
# sort the numbers in ascending order to facilitate binary search
nums.sort()
for x in range(len(nums)):
# find the maximum sum of two elements that is less than k, with the first element being nums[x]
y = search(nums, k - nums[x], x + 1)
if y > x:
# update max_sum with the maximum sum found so far
max_sum = max(max_sum, nums[x] + nums[y])
return max_sum
def search(nums: List[int], target: int, start: int) -> int:
"""
Searches for a number that is less than the target in a sorted list of numbers.
Uses binary search to achieve a time complexity of O(log n) and find the index j such that the sum
nums[i]+nums[j] < k. It takes the nums array, target value, and the start range of the search as input.
Args:
nums (List[int]): A sorted list of integers
target int: The target value to search for
start int: The starting index of the search range
Returns:
The index of the number that is less than the target, or -1 if no such number is found
"""
left, right = start, len(nums) - 1
result = -1
while left <= right:
# calculate the midpoint of the search range
mid = (left + right) // 2
if nums[mid] < target:
# update result to mid and move left to mid + 1 to look for larger values.
result = mid
left = mid + 1
else:
# move right to mid - 1 to check smaller values.
right = mid - 1
return result