31 · Backtracking

#39 Combination Sum

Mediumsolve on LeetCode ↗

frequently reported atWalmartByteDance / TikTokAirbnbAmazonMicrosoftUber

the problem

In plain words

Given distinct positive candidates and a target, return all unique combinations that sum to target. Each candidate may be used any number of times. Two combinations are the same if they use the same numbers the same number of times.

in candidates = [2,3,6,7], target = 7

out [[2,2,3],[7]]

in candidates = [2,3,5], target = 8

out [[2,2,2,2],[2,3,3],[3,5]]

in candidates = [2], target = 1

out []

  • 1 ≤ candidates.length ≤ 30
  • 2 ≤ candidates[i] ≤ 40, all distinct
  • 1 ≤ target ≤ 40

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #40
      Combination Sum II
      Mediumin Backtracking
    2. #518
      Coin Change II
      Mediumin Dynamic Programming
    3. #377
      Combination Sum IV
      Mediumin Dynamic Programming