Data Structures and Algorithms: Combination Sum I, II, III

beginner
5 min

Data Structures and Algorithms: Combination Sum I, II, III

Welcome to our deep dive into the fascinating world of Data Structures and Algorithms! Today, we'll explore three exciting problems, Combination Sum I, Combination Sum II, and Combination Sum III. These problems are a great introduction to backtracking, a powerful algorithmic technique used in various areas of computer science.

Let's start with some preliminaries:

  • Combination: Combinations are a selection of items from a larger set, without regard to the order in which they are selected.
  • Sum: A sum is the total of the numbers in a set or sequence.

šŸ’” Pro Tip: While we focus on combinations, you'll find that these problems also involve basic data structures like arrays and sets.

Combination Sum I (CS1)

Problem Statement

Given a set of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sum up to target. The same repeated number may be selected multiple times.

Example

Let's consider candidates = [2, 3, 6, 7] and target = 7. The solution is:

  • [7]
  • [2, 2, 3]
  • [2, 2, 2, 1]

Solution Approach

  • Start from the smallest candidate number.
  • For each candidate, check if it's greater than target, if so, move to the next candidate.
  • If it's less than or equal to target, add it to the current combination and recursively call the function for the remaining target and remaining candidates.
  • If the remaining target is 0, the current combination is a solution.

Code Example (Python)

python
def combinationSum1(candidates, target): combinations = [] def backtrack(candidates, target, current_combination, start): if target < 0: return elif target == 0: combinations.append(current_combination) else: for i in range(start, len(candidates)): backtrack(candidates, target - candidates[i], current_combination + [candidates[i]], i) candidates.sort() backtrack(candidates, target, [], 0) return combinations

šŸ“ Note: The sorting of candidates ensures that we don't repeat combinations with the same numbers but in a different order.

Quiz

Quick Quiz
Question 1 of 1

Given candidates = [2, 3, 5] and target = 8, what is the output of the `combinationSum1` function?


Combination Sum II (CS2)

Problem Statement

Given a set of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sum up to target. The same repeated number may NOT be selected multiple times.

Example

Let's consider candidates = [10, 1, 2, 7, 6, 1, 5] and target = 8. The solution is:

  • [1, 1, 6]
  • [1, 2, 5]
  • [1, 7]

Solution Approach

  • Similar to CS1, start from the smallest candidate number.
  • For each candidate, check if it's greater than target, if so, move to the next candidate.
  • If it's less than or equal to target, add it to the current combination and recursively call the function for the remaining target and remaining candidates.
  • Unlike CS1, if the current candidate is a repetition of a previous candidate in the current combination, skip it.
  • If the remaining target is 0, the current combination is a solution.

Code Example (Python)

python
def combinationSum2(candidates, target): combinations = [] candidates_set = set(candidates) def backtrack(candidates, target, current_combination, start): if target < 0: return elif target == 0: combinations.append(tuple(current_combination)) # Convert to tuple to avoid duplicates due to order else: for i in range(start, len(candidates)): if candidates[i] > target: break if candidates[i] == candidates[i - 1]: continue backtrack(candidates, target - candidates[i], current_combination + [candidates[i]], i + 1) candidates.sort() backtrack(candidates, target, [], 0) return combinations

šŸ“ Note: We use a set to store candidates and compare with candidates_set[i - 1] to check for duplicates. Also, we use tuples instead of lists for combinations to avoid duplicate combinations due to order.

Quiz

Quick Quiz
Question 1 of 1

Given candidates = [1, 2, 2, 3, 4] and target = 6, what is the output of the `combinationSum2` function?


Combination Sum III (CS3)

Problem Statement

Given a set of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sum up to target. However, each selected candidate must be a multiple of 3.

Example

Let's consider candidates = [1, 3, 6] and target = 4. The solution is:

  • []
  • [3]

Solution Approach

  • Start from the smallest candidate number that is a multiple of 3.
  • Check if it's greater than target, if so, move to the next multiple of 3.
  • If it's less than or equal to target, add it to the current combination and recursively call the function for the remaining target and remaining candidates.
  • If the remaining target is 0, the current combination is a solution.

Code Example (Python)

python
def combinationSum3(candidates, target): combinations = [] def backtrack(candidates, target, current_combination, start): if target < 0: return elif target == 0: combinations.append(tuple(current_combination)) else: for i in range(start, len(candidates)): if candidates[i] % 3 != 0: continue backtrack(candidates, target - candidates[i], current_combination + [candidates[i]], i + 1) candidates_sorted = sorted(filter(lambda x: x % 3 == 0, candidates)) backtrack(candidates_sorted, target, [], 0) return combinations

šŸ“ Note: We use a filtered list of candidates that are multiples of 3.

Quiz

Quick Quiz
Question 1 of 1

Given candidates = [1, 3, 9, 12] and target = 9, what is the output of the `combinationSum3` function?

That's it for today! We hope you enjoyed learning about the Combination Sum problems. Remember, practice makes perfect, so try solving these problems with different sets of candidates and targets. Happy coding! šŸŽÆ