-
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
|
|
||
|
|
||
| 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 | ||
Binary file added
BIN
+51.6 KB
algorithms/arrays/two_sum_less_k/images/examples/two_sum_less_k_example_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+56.5 KB
algorithms/arrays/two_sum_less_k/images/examples/two_sum_less_k_example_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+56 KB
algorithms/arrays/two_sum_less_k/images/examples/two_sum_less_k_example_3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+55.4 KB
algorithms/arrays/two_sum_less_k/images/examples/two_sum_less_k_example_4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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
numsshould be "A sorted list of integers", but the function sorts the input on line 18. The input does not need to be pre-sorted.Update the docstring to reflect the actual requirement:
🤖 Prompt for AI Agents