|
| 1 | +<h2><a href="https://leetcode.com/problems/combination-sum">39. Combination Sum</a></h2><h3>Medium</h3><hr><p>Given an array of <strong>distinct</strong> integers <code>candidates</code> and a target integer <code>target</code>, return <em>a list of all <strong>unique combinations</strong> of </em><code>candidates</code><em> where the chosen numbers sum to </em><code>target</code><em>.</em> You may return the combinations in <strong>any order</strong>.</p> |
| 2 | + |
| 3 | +<p>The <strong>same</strong> number may be chosen from <code>candidates</code> an <strong>unlimited number of times</strong>. Two combinations are unique if the <span data-keyword="frequency-array">frequency</span> of at least one of the chosen numbers is different.</p> |
| 4 | + |
| 5 | +<p>The test cases are generated such that the number of unique combinations that sum up to <code>target</code> is less than <code>150</code> combinations for the given input.</p> |
| 6 | + |
| 7 | +<p> </p> |
| 8 | +<p><strong class="example">Example 1:</strong></p> |
| 9 | + |
| 10 | +<pre> |
| 11 | +<strong>Input:</strong> candidates = [2,3,6,7], target = 7 |
| 12 | +<strong>Output:</strong> [[2,2,3],[7]] |
| 13 | +<strong>Explanation:</strong> |
| 14 | +2 and 3 are candidates, and 2 + 2 + 3 = 7. Note that 2 can be used multiple times. |
| 15 | +7 is a candidate, and 7 = 7. |
| 16 | +These are the only two combinations. |
| 17 | +</pre> |
| 18 | + |
| 19 | +<p><strong class="example">Example 2:</strong></p> |
| 20 | + |
| 21 | +<pre> |
| 22 | +<strong>Input:</strong> candidates = [2,3,5], target = 8 |
| 23 | +<strong>Output:</strong> [[2,2,2,2],[2,3,3],[3,5]] |
| 24 | +</pre> |
| 25 | + |
| 26 | +<p><strong class="example">Example 3:</strong></p> |
| 27 | + |
| 28 | +<pre> |
| 29 | +<strong>Input:</strong> candidates = [2], target = 1 |
| 30 | +<strong>Output:</strong> [] |
| 31 | +</pre> |
| 32 | + |
| 33 | +<p> </p> |
| 34 | +<p><strong>Constraints:</strong></p> |
| 35 | + |
| 36 | +<ul> |
| 37 | + <li><code>1 <= candidates.length <= 30</code></li> |
| 38 | + <li><code>2 <= candidates[i] <= 40</code></li> |
| 39 | + <li>All elements of <code>candidates</code> are <strong>distinct</strong>.</li> |
| 40 | + <li><code>1 <= target <= 40</code></li> |
| 41 | +</ul> |
0 commit comments