754. Combination Sum II (Each Element Used Once)

MediumRecursionRecursion

Given an array of positive integers candidates (which MAY contain duplicates) and a positive integer target, return all unique combinations of candidates whose elements sum to exactly target. Each element may be used AT MOST ONCE in each combination. The output must be canonicalized:
1. Each combination is in non-decreasing order.
2. The list of combinations is sorted lexicographically.
3. Duplicate combinations are removed.

Input: An array of positive integers and a target, formatted as "[c1,c2,...], target".

Output: Canonically sorted list of distinct combinations.

Examples

Example 1
Input: [10,1,2,7,6,1,5], 8
Output: [[1,1,6],[1,2,5],[1,7],[2,6]]
Explanation: Four distinct combinations sorted lex.
Example 2
Input: [2,5,2,1,2], 5
Output: [[1,2,2],[5]]
Explanation: Two distinct combinations; note {2,2,1} has been sorted.
Example 3
Input: [1], 2
Output: []
Explanation: No way to make 2 with a single 1 used once.

Constraints

Asked by

AdobeOracleBloombergMetaMicrosoftAmazon
Solve this problem in the editor →