|
5 | 5 | /* |
6 | 6 | A = [1, 2, 3, 3] |
7 | 7 | B = [2, 3, 3, 4] |
8 | | -C = [1, 2, 2, 2] |
| 8 | +C = [2, 3, 3, 4] |
9 | 9 | target = 7 |
10 | 10 | */ |
11 | 11 |
|
|
16 | 16 | import itertools |
17 | 17 | from functools import partial |
18 | 18 |
|
19 | | -A = [1, 2, 3, 3] |
20 | | -B = [2, 3, 3, 4] |
21 | | -C = [1, 2, 2, 2] |
22 | | -target = 7 |
23 | | - |
24 | | - |
25 | | -def construct_candidates(constructed_sofar): |
26 | | - global A, B, C |
27 | | - array = A |
28 | | - if 1 == len(constructed_sofar): |
29 | | - array = B |
30 | | - elif 2 == len(constructed_sofar): |
31 | | - array = C |
32 | | - return array |
33 | | - |
34 | | - |
35 | | -def over(constructed_sofar): |
36 | | - global target |
37 | | - sum = 0 |
38 | | - to_stop, reached_target = False, False |
39 | | - for elem in constructed_sofar: |
40 | | - sum += elem |
41 | | - if sum >= target or len(constructed_sofar) >= 3: |
42 | | - to_stop = True |
43 | | - if sum == target and 3 == len(constructed_sofar): |
44 | | - reached_target = True |
45 | | - |
46 | | - return to_stop, reached_target |
47 | | - |
48 | | - |
49 | | -def backtrack(constructed_sofar): |
50 | | - to_stop, reached_target = over(constructed_sofar) |
51 | | - if to_stop: |
52 | | - if reached_target: |
53 | | - print(constructed_sofar) |
54 | | - return |
55 | | - candidates = construct_candidates(constructed_sofar) |
56 | | - for candidate in candidates: |
57 | | - constructed_sofar.append(candidate) |
58 | | - backtrack(constructed_sofar[:]) |
59 | | - constructed_sofar.pop() |
60 | | - |
61 | | - |
62 | | -backtrack([]) |
63 | | - |
64 | | - |
65 | | -# Complexity: O(n(m+p)) |
66 | | - |
67 | | -# 1. Sort all the arrays - a,b,c. - This will improve average time complexity. |
68 | | -# 2. If c[i] < Sum, then look for Sum - c[i] in array a and b. When pair found, |
69 | | -# insert c[i], a[j] & b[k] into the result list. This can be done in O(n). |
70 | | -# 3. Keep on doing the above procedure while going through complete c array. |
71 | | - |
72 | | - |
73 | | -A = [1, 2, 3, 3] |
74 | | -B = [2, 3, 3, 4] |
75 | | -C = [1, 2, 2, 2] |
76 | | -S = 7 |
77 | | - |
78 | | - |
79 | | -def check_sum(n, *nums): |
80 | | - if sum(x for x in nums) == n: |
81 | | - return (True, nums) |
82 | | - else: |
83 | | - return (False, nums) |
84 | | - |
85 | | - |
86 | | -pro = itertools.product(A, B, C) |
87 | | -func = partial(check_sum, S) |
88 | | -sums = list(itertools.starmap(func, pro)) |
89 | 19 |
|
90 | | -res = set() |
91 | | -for s in sums: |
92 | | - if s[0] is True and s[1] not in res: |
93 | | - res.add(s[1]) |
94 | | -print(res) |
| 20 | +def array_sum_combinations(A, B, C, target): |
| 21 | + |
| 22 | + def over(constructed_sofar): |
| 23 | + sum = 0 |
| 24 | + to_stop, reached_target = False, False |
| 25 | + for elem in constructed_sofar: |
| 26 | + sum += elem |
| 27 | + if sum >= target or len(constructed_sofar) >= 3: |
| 28 | + to_stop = True |
| 29 | + if sum == target and 3 == len(constructed_sofar): |
| 30 | + reached_target = True |
| 31 | + return to_stop, reached_target |
| 32 | + |
| 33 | + def construct_candidates(constructed_sofar): |
| 34 | + array = A |
| 35 | + if 1 == len(constructed_sofar): |
| 36 | + array = B |
| 37 | + elif 2 == len(constructed_sofar): |
| 38 | + array = C |
| 39 | + return array |
| 40 | + |
| 41 | + def backtrack(constructed_sofar=[], res=[]): |
| 42 | + to_stop, reached_target = over(constructed_sofar) |
| 43 | + if to_stop: |
| 44 | + if reached_target: |
| 45 | + res.append(constructed_sofar) |
| 46 | + return |
| 47 | + candidates = construct_candidates(constructed_sofar) |
| 48 | + |
| 49 | + for candidate in candidates: |
| 50 | + constructed_sofar.append(candidate) |
| 51 | + backtrack(constructed_sofar[:], res) |
| 52 | + constructed_sofar.pop() |
| 53 | + |
| 54 | + res = [] |
| 55 | + backtrack([], res) |
| 56 | + return res |
| 57 | + |
| 58 | + |
| 59 | +def unique_array_sum_combinations(A, B, C, target): |
| 60 | + """ |
| 61 | + 1. Sort all the arrays - a,b,c. - This improves average time complexity. |
| 62 | + 2. If c[i] < Sum, then look for Sum - c[i] in array a and b. |
| 63 | + When pair found, insert c[i], a[j] & b[k] into the result list. |
| 64 | + This can be done in O(n). |
| 65 | + 3. Keep on doing the above procedure while going through complete c array. |
| 66 | +
|
| 67 | + Complexity: O(n(m+p)) |
| 68 | + """ |
| 69 | + def check_sum(n, *nums): |
| 70 | + if sum(x for x in nums) == n: |
| 71 | + return (True, nums) |
| 72 | + else: |
| 73 | + return (False, nums) |
| 74 | + |
| 75 | + pro = itertools.product(A, B, C) |
| 76 | + func = partial(check_sum, target) |
| 77 | + sums = list(itertools.starmap(func, pro)) |
| 78 | + |
| 79 | + res = set() |
| 80 | + for s in sums: |
| 81 | + if s[0] is True and s[1] not in res: |
| 82 | + res.add(s[1]) |
| 83 | + |
| 84 | + return list(res) |
0 commit comments