Subset Sum
Implement subsetSum
Given an array of non-negative integers and a target amount, determine whether any subset of the array — any selection of its elements, in any combination, each used at most once — adds up to exactly that target. The subset doesn't need to be contiguous, and the empty subset always sums to 0.
Every element only ever faces one decision: it's either part of the chosen subset or it isn't. Skipping it leaves the target unchanged for the rest of the array to solve; taking it lowers the target by that element's value for the rest of the array to solve. A target of exactly 0 at any point means a valid subset has already been assembled. Because each element is considered once and only ever shrinks the remaining target (never negative amounts contributing further), the reachable amounts can be tracked directly — building up, one element at a time, the full set of totals that are actually achievable.
Example 1:
Input: nums = [2,3,4], target = 6
Output: true
Example 2:
Input: nums = [2,7,11,4], target = 13
Output: true
Example 3:
Input: nums = [3,7,5], target = 1
Output: false
+ 7 hidden test cases run on Submit.
Constraints:
- ●
1 ≤ nums.length ≤ 20 - ●
0 ≤ nums[i] ≤ 100 - ●
0 ≤ target ≤ 1000
nums =
[2, 3, 4]
target =
6