-
Notifications
You must be signed in to change notification settings - Fork 2
feat(arrays, binary-search): two sum less k #109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| # Two Sum Less Than K | ||
|
|
||
| Given an array of integers, nums, and an integer k, find the maximum sum of two elements in nums less than k. Otherwise, | ||
| return −1 if no such pair exists. | ||
|
|
||
| Constraints | ||
|
|
||
| - 1 ≤ nums.length ≤ 100 | ||
| - 1 ≤ nums[i] ≤ 10^3 | ||
| - 1 ≤ k ≤ 10^3 | ||
|
|
||
| ## Examples | ||
|
|
||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|
|
||
| ## Related Topics | ||
|
|
||
| - Array | ||
| - Two Pointers | ||
| - Binary Search | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,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 | ||
|
Comment on lines
+4
to
+27
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major Fix misleading docstring about input requirements. Line 10 states that Update the docstring to reflect the actual requirement: Args:
- nums (List[int]): A sorted list of integers
+ nums (List[int]): A list of integers (will be sorted internally)
k int: The target value to search for🤖 Prompt for AI Agents |
||
|
|
||
|
|
||
| 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 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import unittest | ||
| from . import two_sum_less_than_k | ||
|
|
||
|
|
||
| class TwoSumLessKTestCase(unittest.TestCase): | ||
| def test_1(self): | ||
| """numbers = [4,2,11,2,5,3,5,8], target = 7""" | ||
| numbers = [4,2,11,2,5,3,5,8] | ||
| target = 7 | ||
| expected = 6 | ||
| actual = two_sum_less_than_k(numbers, target) | ||
| self.assertEqual(expected, actual) | ||
|
|
||
| def test_2(self): | ||
| """numbers = [10,20,30], target = 15""" | ||
| numbers = [10, 20, 30] | ||
| target = 15 | ||
| expected = -1 | ||
| actual = two_sum_less_than_k(numbers, target) | ||
| self.assertEqual(expected, actual) | ||
|
|
||
| def test_3(self): | ||
| """numbers = [34,23,1,24,75,33,54,8], k = 60""" | ||
| numbers = [34,23,1,24,75,33,54,8] | ||
| k = 60 | ||
| expected = 58 | ||
| actual = two_sum_less_than_k(numbers, k) | ||
| self.assertEqual(expected, actual) | ||
|
|
||
| def test_4(self): | ||
| """numbers = [5,5,5,5,5,5], k = 15""" | ||
| numbers = [5,5,5,5,5,5] | ||
| k = 15 | ||
| expected = 10 | ||
| actual = two_sum_less_than_k(numbers, k) | ||
| self.assertEqual(expected, actual) | ||
|
|
||
| def test_5(self): | ||
| """numbers = [1,2,3,4,5], k = 3""" | ||
| numbers = [1,2,3,4,5] | ||
| k = 3 | ||
| expected = -1 | ||
| actual = two_sum_less_than_k(numbers, k) | ||
| self.assertEqual(expected, actual) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
Uh oh!
There was an error while loading. Please reload this page.