From 630d69c8424ffc1c36c9be2290f2cb089391aa08 Mon Sep 17 00:00:00 2001 From: Shubham Kondekar <40213815+kondekarshubham123@users.noreply.github.com> Date: Wed, 26 Oct 2022 19:37:02 +0530 Subject: [PATCH 01/14] Create combination_sum_iv.py --- dynamic_programming/combination_sum_iv.py | 102 ++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 dynamic_programming/combination_sum_iv.py diff --git a/dynamic_programming/combination_sum_iv.py b/dynamic_programming/combination_sum_iv.py new file mode 100644 index 000000000000..c4493cac6345 --- /dev/null +++ b/dynamic_programming/combination_sum_iv.py @@ -0,0 +1,102 @@ +""" +Question: +You are given an array of distinct integers and you have to tell how many +different ways of selecting the elements from the array are there such that +the sum of chosen elements is equal to the target number tar. + +Example + +Input: +N = 3 +target = 5 +array = [1, 2, 5] + +Output: +9 + +Approach: +The basic idea is to go over recursively to find the way such that the sum +of chosen elements is “tar”. For every element, we have two choices + 1. Include the element in our set of chosen elements. + 2. Don’t include the element in our set of chosen elements. +""" + + +from typing import List + + +def combination_sum_iv(N: int, array: List[int], target: int) -> int: + """ + Function checks the all possible combinations, and returns the count + of possible combination in exponential Time Complexity. + + >>> combination_sum_iv(3, [1,2,5], 5) + 9 + """ + def count_of_possible_combinations(target: int) -> int: + if target < 0: + return 0 + if target == 0: + return 1 + count = 0 + for i in range(len(array)): + count += count_of_possible_combinations(target - array[i]) + return count + return count_of_possible_combinations(target) + + +def combination_sum_iv_dp_array(N: int, array: List[int], target: int) -> int: + """ + Function checks the all possible combinations, and returns the count + of possible combination in O(N^2) Time Complexity as we are using Dynamic + programming array here. + + >>> combination_sum_iv_dp_array(3, [1,2,5], 5) + 9 + """ + def count_of_possible_combinations_with_dp_array(target: int, dp_array: List[int]) -> int: + if target < 0: + return 0 + if target == 0: + return 1 + if dp_array[target] != -1: + return dp_array[target] + answer = 0 + for i in range(len(array)): + answer += count_of_possible_combinations_with_dp_array( + target - array[i], dp_array) + dp_array[target] = answer + return answer + dp_array = [-1 for _ in range(target+1)] + return count_of_possible_combinations_with_dp_array(target, dp_array) + + +def combination_sum_iv_bottom_up(N: int, array: List[int], target: int) -> int: + """ + Function checks the all possible combinations with using bottom up approach, + and returns the count of possible combination in O(N^2) Time Complexity + as we are using Dynamic programming array here. + + >>> combination_sum_iv_bottom_up(3, [1,2,5], 5) + 9 + """ + + dp_array = [0 for _ in range(target + 1)] + dp_array[0] = 1 + + for i in range(1, target+1): + for j in range(N): + if (i - array[j] >= 0): + dp_array[i] += dp_array[i - array[j]] + + return dp_array[target] + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + N = 3 + target = 5 + array = [1, 2, 5] + print(combination_sum_iv(N, array, target)) From 909c704d47f7c81cab314efdce59575cf7a39767 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 26 Oct 2022 14:09:43 +0000 Subject: [PATCH 02/14] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dynamic_programming/combination_sum_iv.py | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/dynamic_programming/combination_sum_iv.py b/dynamic_programming/combination_sum_iv.py index c4493cac6345..7bfee928b879 100644 --- a/dynamic_programming/combination_sum_iv.py +++ b/dynamic_programming/combination_sum_iv.py @@ -25,7 +25,7 @@ from typing import List -def combination_sum_iv(N: int, array: List[int], target: int) -> int: +def combination_sum_iv(N: int, array: list[int], target: int) -> int: """ Function checks the all possible combinations, and returns the count of possible combination in exponential Time Complexity. @@ -33,6 +33,7 @@ def combination_sum_iv(N: int, array: List[int], target: int) -> int: >>> combination_sum_iv(3, [1,2,5], 5) 9 """ + def count_of_possible_combinations(target: int) -> int: if target < 0: return 0 @@ -42,10 +43,11 @@ def count_of_possible_combinations(target: int) -> int: for i in range(len(array)): count += count_of_possible_combinations(target - array[i]) return count + return count_of_possible_combinations(target) -def combination_sum_iv_dp_array(N: int, array: List[int], target: int) -> int: +def combination_sum_iv_dp_array(N: int, array: list[int], target: int) -> int: """ Function checks the all possible combinations, and returns the count of possible combination in O(N^2) Time Complexity as we are using Dynamic @@ -54,7 +56,10 @@ def combination_sum_iv_dp_array(N: int, array: List[int], target: int) -> int: >>> combination_sum_iv_dp_array(3, [1,2,5], 5) 9 """ - def count_of_possible_combinations_with_dp_array(target: int, dp_array: List[int]) -> int: + + def count_of_possible_combinations_with_dp_array( + target: int, dp_array: list[int] + ) -> int: if target < 0: return 0 if target == 0: @@ -64,14 +69,16 @@ def count_of_possible_combinations_with_dp_array(target: int, dp_array: List[int answer = 0 for i in range(len(array)): answer += count_of_possible_combinations_with_dp_array( - target - array[i], dp_array) + target - array[i], dp_array + ) dp_array[target] = answer return answer - dp_array = [-1 for _ in range(target+1)] + + dp_array = [-1 for _ in range(target + 1)] return count_of_possible_combinations_with_dp_array(target, dp_array) -def combination_sum_iv_bottom_up(N: int, array: List[int], target: int) -> int: +def combination_sum_iv_bottom_up(N: int, array: list[int], target: int) -> int: """ Function checks the all possible combinations with using bottom up approach, and returns the count of possible combination in O(N^2) Time Complexity @@ -84,9 +91,9 @@ def combination_sum_iv_bottom_up(N: int, array: List[int], target: int) -> int: dp_array = [0 for _ in range(target + 1)] dp_array[0] = 1 - for i in range(1, target+1): + for i in range(1, target + 1): for j in range(N): - if (i - array[j] >= 0): + if i - array[j] >= 0: dp_array[i] += dp_array[i - array[j]] return dp_array[target] From c63e28e6288825b0d76a1e73f2c0381894055165 Mon Sep 17 00:00:00 2001 From: Shubham Kondekar <40213815+kondekarshubham123@users.noreply.github.com> Date: Wed, 26 Oct 2022 20:22:40 +0530 Subject: [PATCH 03/14] Update dynamic_programming/combination_sum_iv.py Co-authored-by: Caeden Perelli-Harris --- dynamic_programming/combination_sum_iv.py | 1 - 1 file changed, 1 deletion(-) diff --git a/dynamic_programming/combination_sum_iv.py b/dynamic_programming/combination_sum_iv.py index 7bfee928b879..1b38e4b7f5f2 100644 --- a/dynamic_programming/combination_sum_iv.py +++ b/dynamic_programming/combination_sum_iv.py @@ -22,7 +22,6 @@ """ -from typing import List def combination_sum_iv(N: int, array: list[int], target: int) -> int: From 1c35f5caf00f10fbe65487f80389c32b6dd95ee3 Mon Sep 17 00:00:00 2001 From: Shubham Kondekar <40213815+kondekarshubham123@users.noreply.github.com> Date: Wed, 26 Oct 2022 20:23:00 +0530 Subject: [PATCH 04/14] Update dynamic_programming/combination_sum_iv.py Co-authored-by: Caeden Perelli-Harris --- dynamic_programming/combination_sum_iv.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dynamic_programming/combination_sum_iv.py b/dynamic_programming/combination_sum_iv.py index 1b38e4b7f5f2..76634118cd39 100644 --- a/dynamic_programming/combination_sum_iv.py +++ b/dynamic_programming/combination_sum_iv.py @@ -73,7 +73,7 @@ def count_of_possible_combinations_with_dp_array( dp_array[target] = answer return answer - dp_array = [-1 for _ in range(target + 1)] + dp_array = [-1] * (target + 1) return count_of_possible_combinations_with_dp_array(target, dp_array) From 81bd35f51983ceda04fdcbed57d49eefc2062dac Mon Sep 17 00:00:00 2001 From: Shubham Kondekar <40213815+kondekarshubham123@users.noreply.github.com> Date: Wed, 26 Oct 2022 20:23:24 +0530 Subject: [PATCH 05/14] Update dynamic_programming/combination_sum_iv.py Co-authored-by: Caeden Perelli-Harris --- dynamic_programming/combination_sum_iv.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dynamic_programming/combination_sum_iv.py b/dynamic_programming/combination_sum_iv.py index 76634118cd39..250a1c045d66 100644 --- a/dynamic_programming/combination_sum_iv.py +++ b/dynamic_programming/combination_sum_iv.py @@ -87,7 +87,7 @@ def combination_sum_iv_bottom_up(N: int, array: list[int], target: int) -> int: 9 """ - dp_array = [0 for _ in range(target + 1)] + dp_array = [0] * (target + 1) dp_array[0] = 1 for i in range(1, target + 1): From 463f99ec882a02da210690122cef81c0d514629e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 26 Oct 2022 14:54:08 +0000 Subject: [PATCH 06/14] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dynamic_programming/combination_sum_iv.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/dynamic_programming/combination_sum_iv.py b/dynamic_programming/combination_sum_iv.py index 250a1c045d66..99d7a2b6bfa6 100644 --- a/dynamic_programming/combination_sum_iv.py +++ b/dynamic_programming/combination_sum_iv.py @@ -22,8 +22,6 @@ """ - - def combination_sum_iv(N: int, array: list[int], target: int) -> int: """ Function checks the all possible combinations, and returns the count @@ -73,7 +71,7 @@ def count_of_possible_combinations_with_dp_array( dp_array[target] = answer return answer - dp_array = [-1] * (target + 1) + dp_array = [-1] * (target + 1) return count_of_possible_combinations_with_dp_array(target, dp_array) From 194ff68283d6b1518f60dae924cb2cab901c3b38 Mon Sep 17 00:00:00 2001 From: Shubham Kondekar <40213815+kondekarshubham123@users.noreply.github.com> Date: Wed, 26 Oct 2022 20:43:06 +0530 Subject: [PATCH 07/14] Update combination_sum_iv.py --- dynamic_programming/combination_sum_iv.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/dynamic_programming/combination_sum_iv.py b/dynamic_programming/combination_sum_iv.py index 99d7a2b6bfa6..4dff586bd228 100644 --- a/dynamic_programming/combination_sum_iv.py +++ b/dynamic_programming/combination_sum_iv.py @@ -22,7 +22,7 @@ """ -def combination_sum_iv(N: int, array: list[int], target: int) -> int: +def combination_sum_iv(n: int, array: list[int], target: int) -> int: """ Function checks the all possible combinations, and returns the count of possible combination in exponential Time Complexity. @@ -44,7 +44,7 @@ def count_of_possible_combinations(target: int) -> int: return count_of_possible_combinations(target) -def combination_sum_iv_dp_array(N: int, array: list[int], target: int) -> int: +def combination_sum_iv_dp_array(n: int, array: list[int], target: int) -> int: """ Function checks the all possible combinations, and returns the count of possible combination in O(N^2) Time Complexity as we are using Dynamic @@ -75,7 +75,7 @@ def count_of_possible_combinations_with_dp_array( return count_of_possible_combinations_with_dp_array(target, dp_array) -def combination_sum_iv_bottom_up(N: int, array: list[int], target: int) -> int: +def combination_sum_iv_bottom_up(n: int, array: list[int], target: int) -> int: """ Function checks the all possible combinations with using bottom up approach, and returns the count of possible combination in O(N^2) Time Complexity @@ -100,7 +100,7 @@ def combination_sum_iv_bottom_up(N: int, array: list[int], target: int) -> int: import doctest doctest.testmod() - N = 3 + n = 3 target = 5 array = [1, 2, 5] - print(combination_sum_iv(N, array, target)) + print(combination_sum_iv(n, array, target)) From 6435bef3206a4a7c5eb29029a34e59b865455d55 Mon Sep 17 00:00:00 2001 From: Shubham Kondekar <40213815+kondekarshubham123@users.noreply.github.com> Date: Wed, 26 Oct 2022 21:06:12 +0530 Subject: [PATCH 08/14] Update combination_sum_iv.py --- dynamic_programming/combination_sum_iv.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dynamic_programming/combination_sum_iv.py b/dynamic_programming/combination_sum_iv.py index 4dff586bd228..def23075d0c0 100644 --- a/dynamic_programming/combination_sum_iv.py +++ b/dynamic_programming/combination_sum_iv.py @@ -89,7 +89,7 @@ def combination_sum_iv_bottom_up(n: int, array: list[int], target: int) -> int: dp_array[0] = 1 for i in range(1, target + 1): - for j in range(N): + for j in range(n): if i - array[j] >= 0: dp_array[i] += dp_array[i - array[j]] From 9817a4407dad59035b87fbbf2af6ecf0d016e3bd Mon Sep 17 00:00:00 2001 From: Shubham Kondekar <40213815+kondekarshubham123@users.noreply.github.com> Date: Fri, 28 Oct 2022 20:12:15 +0530 Subject: [PATCH 09/14] Resolved PR Comments --- dynamic_programming/combination_sum_iv.py | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/dynamic_programming/combination_sum_iv.py b/dynamic_programming/combination_sum_iv.py index def23075d0c0..03dbd6a7b8d0 100644 --- a/dynamic_programming/combination_sum_iv.py +++ b/dynamic_programming/combination_sum_iv.py @@ -36,10 +36,7 @@ def count_of_possible_combinations(target: int) -> int: return 0 if target == 0: return 1 - count = 0 - for i in range(len(array)): - count += count_of_possible_combinations(target - array[i]) - return count + return sum([count_of_possible_combinations(target - each) for each in array]) return count_of_possible_combinations(target) @@ -63,11 +60,7 @@ def count_of_possible_combinations_with_dp_array( return 1 if dp_array[target] != -1: return dp_array[target] - answer = 0 - for i in range(len(array)): - answer += count_of_possible_combinations_with_dp_array( - target - array[i], dp_array - ) + answer = sum([count_of_possible_combinations_with_dp_array(target - each) for each in array]) dp_array[target] = answer return answer From de2969319016a8b7d23c63c272cbd1fe8876a25d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 28 Oct 2022 14:44:15 +0000 Subject: [PATCH 10/14] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dynamic_programming/combination_sum_iv.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/dynamic_programming/combination_sum_iv.py b/dynamic_programming/combination_sum_iv.py index 03dbd6a7b8d0..dcaa793941d3 100644 --- a/dynamic_programming/combination_sum_iv.py +++ b/dynamic_programming/combination_sum_iv.py @@ -60,7 +60,12 @@ def count_of_possible_combinations_with_dp_array( return 1 if dp_array[target] != -1: return dp_array[target] - answer = sum([count_of_possible_combinations_with_dp_array(target - each) for each in array]) + answer = sum( + [ + count_of_possible_combinations_with_dp_array(target - each) + for each in array + ] + ) dp_array[target] = answer return answer From d42eafd7aded004b05be73c78da8e11cf989e27c Mon Sep 17 00:00:00 2001 From: Shubham Kondekar <40213815+kondekarshubham123@users.noreply.github.com> Date: Fri, 28 Oct 2022 20:18:06 +0530 Subject: [PATCH 11/14] minor change, argument missing in function --- dynamic_programming/combination_sum_iv.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/dynamic_programming/combination_sum_iv.py b/dynamic_programming/combination_sum_iv.py index dcaa793941d3..a81aa2c6a9b1 100644 --- a/dynamic_programming/combination_sum_iv.py +++ b/dynamic_programming/combination_sum_iv.py @@ -60,12 +60,7 @@ def count_of_possible_combinations_with_dp_array( return 1 if dp_array[target] != -1: return dp_array[target] - answer = sum( - [ - count_of_possible_combinations_with_dp_array(target - each) - for each in array - ] - ) + answer = sum([count_of_possible_combinations_with_dp_array(target - each, dp_array) for each in array]) dp_array[target] = answer return answer From 0c3b8cab7c8ffb2212b8b79bc4880fb6469c1e88 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 28 Oct 2022 14:52:17 +0000 Subject: [PATCH 12/14] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dynamic_programming/combination_sum_iv.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/dynamic_programming/combination_sum_iv.py b/dynamic_programming/combination_sum_iv.py index a81aa2c6a9b1..14feb78edc57 100644 --- a/dynamic_programming/combination_sum_iv.py +++ b/dynamic_programming/combination_sum_iv.py @@ -60,7 +60,12 @@ def count_of_possible_combinations_with_dp_array( return 1 if dp_array[target] != -1: return dp_array[target] - answer = sum([count_of_possible_combinations_with_dp_array(target - each, dp_array) for each in array]) + answer = sum( + [ + count_of_possible_combinations_with_dp_array(target - each, dp_array) + for each in array + ] + ) dp_array[target] = answer return answer From 2e846dc354859dd4727b5632c1865530b76b48fd Mon Sep 17 00:00:00 2001 From: Shubham Kondekar <40213815+kondekarshubham123@users.noreply.github.com> Date: Fri, 28 Oct 2022 20:36:35 +0530 Subject: [PATCH 13/14] Update dynamic_programming/combination_sum_iv.py Co-authored-by: Christian Clauss --- dynamic_programming/combination_sum_iv.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dynamic_programming/combination_sum_iv.py b/dynamic_programming/combination_sum_iv.py index 14feb78edc57..76392fec3542 100644 --- a/dynamic_programming/combination_sum_iv.py +++ b/dynamic_programming/combination_sum_iv.py @@ -36,7 +36,7 @@ def count_of_possible_combinations(target: int) -> int: return 0 if target == 0: return 1 - return sum([count_of_possible_combinations(target - each) for each in array]) + return sum(count_of_possible_combinations(target - item) for item in array) return count_of_possible_combinations(target) From 8cf9c1a593396641da64f081d192295e12e34e45 Mon Sep 17 00:00:00 2001 From: Shubham Kondekar <40213815+kondekarshubham123@users.noreply.github.com> Date: Fri, 28 Oct 2022 21:56:28 +0530 Subject: [PATCH 14/14] minor change --- dynamic_programming/combination_sum_iv.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/dynamic_programming/combination_sum_iv.py b/dynamic_programming/combination_sum_iv.py index 76392fec3542..b2aeb0824f64 100644 --- a/dynamic_programming/combination_sum_iv.py +++ b/dynamic_programming/combination_sum_iv.py @@ -61,10 +61,8 @@ def count_of_possible_combinations_with_dp_array( if dp_array[target] != -1: return dp_array[target] answer = sum( - [ - count_of_possible_combinations_with_dp_array(target - each, dp_array) - for each in array - ] + count_of_possible_combinations_with_dp_array(target - item, dp_array) + for item in array ) dp_array[target] = answer return answer