LeetCode 39: Combination Sum Solution

Master LeetCode problem 39 (Combination Sum), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

39. Combination Sum

Problem Explanation

Explanation

To solve this problem, we can use backtracking to generate all possible combinations of candidates that sum up to the target. We start with an empty combination and recursively explore all possibilities by either including or excluding each candidate in the current combination.

Algorithm

  1. Sort the candidates array to easily avoid duplicates in the final result.
  2. Define a recursive backtracking function that takes the current combination, current index in the candidates array, and remaining target as parameters.
  3. In the backtracking function:
    • If the target becomes 0, add the current combination to the result.
    • Iterate over the candidates starting from the current index.
    • For each candidate:
      • Add the candidate to the current combination.
      • Recursively call the function with the updated combination, the same index (allowing duplicates), and the reduced target by subtracting the candidate value.
      • Remove the candidate from the current combination to backtrack and try the next candidate.
  4. Call the backtracking function with an empty combination, starting from the first candidate, and the initial target.
  5. Return the list of unique combinations.

Time Complexity

The time complexity of this approach is O(2^N), where N is the number of candidates. This is because for each candidate, we have two choices - either include it or exclude it.

Space Complexity

The space complexity is O(N) to store the recursive call stack.

Solution Code

import java.util.*;

class Solution {
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> result = new ArrayList<>();
        Arrays.sort(candidates);
        backtrack(result, new ArrayList<>(), candidates, target, 0);
        return result;
    }

    private void backtrack(List<List<Integer>> result, List<Integer> combination, int[] candidates, int target, int start) {
        if (target == 0) {
            result.add(new ArrayList<>(combination));
            return;
        }
        for (int i = start; i < candidates.length; i++) {
            if (candidates[i] > target) {
                break;
            }
            combination.add(candidates[i]);
            backtrack(result, combination, candidates, target - candidates[i], i);
            combination.remove(combination.size() - 1);
        }
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 39 (Combination Sum)?

This page provides optimized solutions for LeetCode problem 39 (Combination Sum) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 39 (Combination Sum)?

The time complexity for LeetCode 39 (Combination Sum) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 39 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 39 in Java, C++, or Python.

Back to LeetCode Solutions