From ba8dac3b9c67f5ce7203c0bc0e50c9f136a93bde Mon Sep 17 00:00:00 2001 From: Pranavkumar Mallela Date: Sat, 7 Oct 2023 22:21:37 +0530 Subject: [PATCH 1/3] add find triplets with 0 sum (3sum) --- .../arrays/find_triplets_with_0_sum.py | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 data_structures/arrays/find_triplets_with_0_sum.py diff --git a/data_structures/arrays/find_triplets_with_0_sum.py b/data_structures/arrays/find_triplets_with_0_sum.py new file mode 100644 index 000000000000..2077949e32a0 --- /dev/null +++ b/data_structures/arrays/find_triplets_with_0_sum.py @@ -0,0 +1,65 @@ +""" +https://medium.com/@eric.christopher.ness/leetcode-15-three-sum-de7e11e25106 +""" + + +def find_triplets_with_0_sum(nums: list[int]) -> list[list[int]]: + """ + Given a list of integers, return elements a, b, c such that a + b + c = 0. + + Args: + nums (list[int]): list of integers + + Returns: + list[list[int]]: list of lists of integers + + Examples: + >>> find_triplets_with_0_sum([-1, 0, 1, 2, -1, -4]) + [[-1, -1, 2], [-1, 0, 1]] + + >>> find_triplets_with_0_sum([]) + [] + + >>> find_triplets_with_0_sum([0, 0, 0]) + [[0, 0, 0]] + + >>> find_triplets_with_0_sum([1, 2, 3, 0, -1, -2, -3]) + [[-3, 0, 3], [-3, 1, 2], [-2, -1, 3], [-2, 0, 2], [-1, 0, 1]] + + """ + # Sort the list in ascending order to use two pointer method. + nums.sort() + + result = [] + + # Iterate through the list and use two-pointer method to initialize the target. + for index in range(len(nums)): + # Skip duplicate elements so that result does not contain duplicate triplets. + if index > 0 and nums[index] == nums[index - 1]: + continue + + # Initialize the target and the left and right pointers. + target = -nums[index] + left = index + 1 + right = len(nums) - 1 + + # Two-pointer method + while left < right: + # If the sum equals target, add the triplet to result. + if nums[left] + nums[right] == target: + result.append([nums[index], nums[left], nums[right]]) + left += 1 + + # Skip duplicate elements. + while left < right and nums[left] == nums[left - 1]: + left += 1 + + # If the sum of is lesser than target, increment left pointer. + elif nums[left] + nums[right] < target: + left += 1 + + # If the sum of is greater than target, decrement right pointer. + else: + right -= 1 + + return result From 7284a447982e0bce77f1b2fdb69a466eacff258c Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Thu, 12 Oct 2023 21:40:53 +0200 Subject: [PATCH 2/3] Update find_triplets_with_0_sum.py --- .../arrays/find_triplets_with_0_sum.py | 53 ++----------------- 1 file changed, 5 insertions(+), 48 deletions(-) diff --git a/data_structures/arrays/find_triplets_with_0_sum.py b/data_structures/arrays/find_triplets_with_0_sum.py index 2077949e32a0..93a16697534e 100644 --- a/data_structures/arrays/find_triplets_with_0_sum.py +++ b/data_structures/arrays/find_triplets_with_0_sum.py @@ -1,65 +1,22 @@ -""" -https://medium.com/@eric.christopher.ness/leetcode-15-three-sum-de7e11e25106 -""" +from itertools import combinations def find_triplets_with_0_sum(nums: list[int]) -> list[list[int]]: """ Given a list of integers, return elements a, b, c such that a + b + c = 0. - Args: - nums (list[int]): list of integers - + nums: list of integers Returns: - list[list[int]]: list of lists of integers - + list of lists of integers where sum(each_list) == 0 Examples: >>> find_triplets_with_0_sum([-1, 0, 1, 2, -1, -4]) [[-1, -1, 2], [-1, 0, 1]] - >>> find_triplets_with_0_sum([]) [] - >>> find_triplets_with_0_sum([0, 0, 0]) [[0, 0, 0]] - >>> find_triplets_with_0_sum([1, 2, 3, 0, -1, -2, -3]) [[-3, 0, 3], [-3, 1, 2], [-2, -1, 3], [-2, 0, 2], [-1, 0, 1]] - """ - # Sort the list in ascending order to use two pointer method. - nums.sort() - - result = [] - - # Iterate through the list and use two-pointer method to initialize the target. - for index in range(len(nums)): - # Skip duplicate elements so that result does not contain duplicate triplets. - if index > 0 and nums[index] == nums[index - 1]: - continue - - # Initialize the target and the left and right pointers. - target = -nums[index] - left = index + 1 - right = len(nums) - 1 - - # Two-pointer method - while left < right: - # If the sum equals target, add the triplet to result. - if nums[left] + nums[right] == target: - result.append([nums[index], nums[left], nums[right]]) - left += 1 - - # Skip duplicate elements. - while left < right and nums[left] == nums[left - 1]: - left += 1 - - # If the sum of is lesser than target, increment left pointer. - elif nums[left] + nums[right] < target: - left += 1 - - # If the sum of is greater than target, decrement right pointer. - else: - right -= 1 - - return result + tuples = sorted(set(abc for abc in combinations(sorted(nums), 3) if not sum(abc))) + return [list(x) for x in tuples] From 68c46d0c317c789e2f71def1f2e0dc0e4479b47d Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Thu, 12 Oct 2023 21:44:13 +0200 Subject: [PATCH 3/3] Update find_triplets_with_0_sum.py --- data_structures/arrays/find_triplets_with_0_sum.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/data_structures/arrays/find_triplets_with_0_sum.py b/data_structures/arrays/find_triplets_with_0_sum.py index 93a16697534e..8217ff857e3d 100644 --- a/data_structures/arrays/find_triplets_with_0_sum.py +++ b/data_structures/arrays/find_triplets_with_0_sum.py @@ -18,5 +18,7 @@ def find_triplets_with_0_sum(nums: list[int]) -> list[list[int]]: >>> find_triplets_with_0_sum([1, 2, 3, 0, -1, -2, -3]) [[-3, 0, 3], [-3, 1, 2], [-2, -1, 3], [-2, 0, 2], [-1, 0, 1]] """ - tuples = sorted(set(abc for abc in combinations(sorted(nums), 3) if not sum(abc))) - return [list(x) for x in tuples] + return [ + list(x) + for x in sorted({abc for abc in combinations(sorted(nums), 3) if not sum(abc)}) + ]