-
Notifications
You must be signed in to change notification settings - Fork 8
/
fourSum.py
43 lines (39 loc) · 1.71 KB
/
fourSum.py
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
from typing import List
def fourNumberSum(array:List[int],targetSum:int)->List[List[int]]:
allPairSums = {} # tracking the number of two sum pairs
quadruplets = [] # return all quadrplets possible
for i in range (1,len(array)-1):
# try to find the first partial sum of the target sum
# and index + 1 -- len(array) before computing the possible partial sum of the
# other aspect of the the target sum
for j in range(i+1,len(array)):
currentSum = array[i] + array[j]
difference = targetSum - currentSum
if difference in allPairSums:
for pair in allPairSums[difference]:
quadruplets.append(pair + [array[j],array[i]])
for k in range(0,i):
currentSum = array[k] + array[i]
if currentSum not in allPairSums:
allPairSums[currentSum] = [array[k],array[i]]
else:
allPairSums[currentSum].append([array[k],array[i]])
return quadruplets
def fourNumberSum(array:List[int],targetSum:int)->List[List[int]]:
result = []
array.sort()
for i in range(len(array)-3):
for j in range(i+1,len(array)-2):
left = j+1
right = len(array)-1
while left < right:
total_sum = array[left] + array[right] + array[i] + array[j]
if total_sum == targetSum:
result.append([array[i], array[j], array[left], array[right]])
left += 1
right -=1
elif total_sum > targetSum:
right -= 1
else:
left += 1
return result