/40. Combination Sum II

40. Combination Sum II

Medium
Arrays59.1% acceptance

Given an integer array nums and an integer sum_target, return all unique combinations of elements from nums that sum to sum_target. Each element in nums can be used at most once in each combination. The result must not contain duplicate combinations. Combinations can be returned in any order.

Example 1

Input: nums = [3, 4, 4, 5, 6], sum_target = 9

Output: [[3, 6], [4, 5]]

Explanation: Possible unique combinations are [3,6] and [4,5].

Example 2

Input: nums = [1, 1, 2, 3], sum_target = 4

Output: [[1, 1, 2], [1, 3], [2, 2]]

Explanation: Possible unique combinations are [1,1,2], [1,3], and [2,2].

Constraints

  • 1 <= len(nums) <= 100
  • 1 <= nums[i] <= 50
  • 1 <= sum_target <= 30
Python (current runtime)

Case 1

Input: nums = [2, 3, 3, 7], sum_target = 8

Expected: [[2, 3, 3], [1, 7]]

Case 2

Input: nums = [5, 5, 2, 1], sum_target = 7

Expected: [[2, 5], [1, 2, 4]]

Case 3

Input: nums = [1, 2, 2, 2, 3], sum_target = 5

Expected: [[1, 2, 2], [2, 3]]