Advertisement

Combination Sum II - LeetCode 40 Solution

Combination Sum II - Complete Solution Guide

Combination Sum II is LeetCode problem 40, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given a collection of candidate numbers ( candidates ) and a target number ( target ), find all unique combinations in candidates where the candidate numbers sum to target . Each number in candidates may only be used once in the combination. Note: The solution set must not contain duplicate combinations. Example 1: Input: candidates = [10,1,2,7,6,1,5], target = 8 Output: [ [1,1,6], [1,2,5], [1,7], [2,6] ] Example 2: Input: candidates = [2,5,2,1,2], target = 5 Output: [ [1,2,2], [5] ] Constraints

Detailed Explanation

The 'Combination Sum II' problem asks us to find all unique combinations of numbers from a given collection ('candidates') that add up to a specific target number. Each number in the 'candidates' list can only be used once in each combination, and the solution must not contain duplicate combinations. The candidates array can contain duplicate numbers, which is a key aspect of this problem that distinguishes it from 'Combination Sum I'. The goal is to return a list of lists, where each inner list represents a unique combination that sums to the target.

Solution Approach

The provided code utilizes a backtracking algorithm to find all unique combinations. The approach involves first sorting the input array 'candidates' to easily handle duplicate numbers and improve efficiency. Then, a recursive 'backtrack' function is used to explore potential combinations. This function builds up a combination by adding elements from the candidates array, checking at each step if the combination's sum equals the target. If it does, the combination is added to the result. If the sum exceeds the target, the current branch is abandoned. The crucial part is the handling of duplicates: the condition `if i > start_index and candidates[i] == candidates[i-1]: continue` prevents adding the same number multiple times in the same level of recursion, thus avoiding duplicate combinations.

Step-by-Step Algorithm

  1. Step 1: Sort the 'candidates' array in ascending order. This step is crucial for efficiently skipping duplicate combinations.
  2. Step 2: Define a recursive 'backtrack' function that takes the 'start_index', 'remaining_target', and 'current_path' as input. 'start_index' indicates from which index of 'candidates' to start considering numbers.
  3. Step 3: In the 'backtrack' function, check if 'remaining_target' is equal to 0. If so, add a copy of the 'current_path' to the 'results' list and return. This signifies that a valid combination has been found.
  4. Step 4: Iterate through the 'candidates' array from 'start_index' to the end. At each iteration, check if the current number is greater than 'remaining_target'. If so, break the loop because the remaining numbers will also be too large.
  5. Step 5: Skip duplicate numbers. If the current number is the same as the previous number (and we are not at the starting index of the loop), skip it to avoid duplicate combinations. This is done with the condition `if i > start_index and candidates[i] == candidates[i-1]: continue`.
  6. Step 6: Add the current number to the 'current_path'.
  7. Step 7: Recursively call the 'backtrack' function with the updated 'start_index' (i + 1), 'remaining_target' (remaining_target - candidates[i]), and 'current_path'.
  8. Step 8: Remove the last added number from the 'current_path' (backtrack) to explore other possible combinations.
  9. Step 9: After the 'backtrack' function finishes exploring all possible combinations, return the 'results' list.

Key Insights

  • Insight 1: Sorting the candidates array allows us to efficiently skip duplicate combinations using the condition `if i > start_index and candidates[i] == candidates[i-1]: continue`.
  • Insight 2: Backtracking is a natural fit for exploring all possible combinations. It allows us to build up combinations incrementally and backtrack when a combination exceeds the target or reaches the end of the candidates list.
  • Insight 3: The `start_index` parameter in the recursive function is crucial for ensuring each number is used only once and for avoiding duplicate combinations. It indicates the index from which to start considering elements in the next level of recursion.

Complexity Analysis

Time Complexity: O(2^n)

Space Complexity: O(n)

Topics

This problem involves: Array, Backtracking.

Companies

Asked at: Adobe, Amazon, Bloomberg, ByteDance, Infosys, Meta, Microsoft, Oracle, Snap, Tesla, TikTok, Uber.