From f143adb639ec5ea9ffa289ba1bab871362c3dbc4 Mon Sep 17 00:00:00 2001 From: Manas Bhilare <69368910+Manas146@users.noreply.github.com> Date: Wed, 6 Oct 2021 11:57:11 +0530 Subject: [PATCH 1/4] Added 4Sums readme file and code --- 0018/4Sums.py | 18 ++++++++++++++++++ 0018/README.md | 27 +++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 0018/4Sums.py create mode 100644 0018/README.md diff --git a/0018/4Sums.py b/0018/4Sums.py new file mode 100644 index 0000000..ea7d0ce --- /dev/null +++ b/0018/4Sums.py @@ -0,0 +1,18 @@ +class Solution: + def fourSum(self, nums: List[int], target: int) -> List[List[int]]: + ans = set() + nums.sort() + for i in range(len(nums) - 3): + for j in range(i + 1, len(nums) - 2): + lo = j + 1 + hi = len(nums) - 1 + while lo < hi: + s = nums[i] + nums[j] + nums[lo] + nums[hi] + if s == target: + ans.add((nums[i], nums[j], nums[lo], nums[hi])) + lo += 1 + elif s < target: + lo += 1 + else: + hi -= 1 + return list(ans) \ No newline at end of file diff --git a/0018/README.md b/0018/README.md new file mode 100644 index 0000000..8ed8deb --- /dev/null +++ b/0018/README.md @@ -0,0 +1,27 @@ +# 4Sums + +Given an array nums of n integers, return an array of all the unique quadruplets [nums[a], nums[b], nums[c], nums[d]] such that: + +0 <= a, b, c, d < n +a, b, c, and d are distinct. +nums[a] + nums[b] + nums[c] + nums[d] == target +You may return the answer in any order. + + + +## Example 1: + +Input: nums = [1,0,-1,0,-2,2], target = 0 +Output: [[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]] + +## Example 2: + +Input: nums = [2,2,2,2,2], target = 8 +Output: [[2,2,2,2]] + + +## Constraints: + +1 <= nums.length <= 200 +-109 <= nums[i] <= 109 +-109 <= target <= 109 \ No newline at end of file From afe36f2072369037bf36c2c6475bef1ab02da9bd Mon Sep 17 00:00:00 2001 From: Manas Bhilare <69368910+Manas146@users.noreply.github.com> Date: Wed, 6 Oct 2021 11:57:46 +0530 Subject: [PATCH 2/4] Update README.md --- 0018/README.md | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/0018/README.md b/0018/README.md index 8ed8deb..d0a59ca 100644 --- a/0018/README.md +++ b/0018/README.md @@ -11,12 +11,18 @@ You may return the answer in any order. ## Example 1: -Input: nums = [1,0,-1,0,-2,2], target = 0 +Input: nums = [1,0,-1,0,-2,2], + +target = 0 + Output: [[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]] ## Example 2: -Input: nums = [2,2,2,2,2], target = 8 +Input: nums = [2,2,2,2,2], + +target = 8 + Output: [[2,2,2,2]] @@ -24,4 +30,4 @@ Output: [[2,2,2,2]] 1 <= nums.length <= 200 -109 <= nums[i] <= 109 --109 <= target <= 109 \ No newline at end of file +-109 <= target <= 109 From 939d7aca16c63ceccd5f98a14dc13d4d1df96c1c Mon Sep 17 00:00:00 2001 From: Manas Bhilare <69368910+Manas146@users.noreply.github.com> Date: Wed, 6 Oct 2021 12:00:43 +0530 Subject: [PATCH 3/4] Update README.md --- 0018/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/0018/README.md b/0018/README.md index d0a59ca..75d0a07 100644 --- a/0018/README.md +++ b/0018/README.md @@ -1,4 +1,4 @@ -# 4Sums +# 4Sum Given an array nums of n integers, return an array of all the unique quadruplets [nums[a], nums[b], nums[c], nums[d]] such that: From e385c7b312dbcb3e19f9ea1f2ae98cd1f517d3ed Mon Sep 17 00:00:00 2001 From: Manas Bhilare <69368910+Manas146@users.noreply.github.com> Date: Thu, 7 Oct 2021 21:37:43 +0530 Subject: [PATCH 4/4] Updated 4Sum code --- 0018/4Sum.py | 33 +++++++++++++++++++++++++++++++++ 0018/4Sums.py | 18 ------------------ 2 files changed, 33 insertions(+), 18 deletions(-) create mode 100644 0018/4Sum.py delete mode 100644 0018/4Sums.py diff --git a/0018/4Sum.py b/0018/4Sum.py new file mode 100644 index 0000000..f58cb8f --- /dev/null +++ b/0018/4Sum.py @@ -0,0 +1,33 @@ +# Two Pointers approach + +class Solution: + def fourSum(self, nums: List[int], target: int) -> List[List[int]]: + + def kSum(nums: List[int], target: int, k: int) -> List[List[int]]: + res = [] + if len(nums) == 0 or nums[0] * k > target or target > nums[-1] * k: + return res + if k == 2: + return twoSum(nums, target) + + for i in range(len(nums)): + if i == 0 or nums[i - 1] != nums[i]: + for subset in kSum(nums[i + 1:], target - nums[i], k - 1): + res.append([nums[i]] + subset) + + return res + + def twoSum(nums: List[int], target: int) -> List[List[int]]: + res = [] + s = set() + + for i in range(len(nums)): + if len(res) == 0 or res[-1][1] != nums[i]: + if target - nums[i] in s: + res.append([target - nums[i], nums[i]]) + s.add(nums[i]) + + return res + + nums.sort() + return kSum(nums, target, 4) diff --git a/0018/4Sums.py b/0018/4Sums.py deleted file mode 100644 index ea7d0ce..0000000 --- a/0018/4Sums.py +++ /dev/null @@ -1,18 +0,0 @@ -class Solution: - def fourSum(self, nums: List[int], target: int) -> List[List[int]]: - ans = set() - nums.sort() - for i in range(len(nums) - 3): - for j in range(i + 1, len(nums) - 2): - lo = j + 1 - hi = len(nums) - 1 - while lo < hi: - s = nums[i] + nums[j] + nums[lo] + nums[hi] - if s == target: - ans.add((nums[i], nums[j], nums[lo], nums[hi])) - lo += 1 - elif s < target: - lo += 1 - else: - hi -= 1 - return list(ans) \ No newline at end of file