2-D Dynamic Programming Knapsack
552. Subset Sum
You are given an array of positive integers arr and a positive integer target. Return true if some
subset of arr adds up to exactly target, and false otherwise.
A subset is any selection of elements from arr. The chosen elements do not have to be next to each other,
and each element can be chosen at most once. Two equal values at different positions are different elements,
so both can be chosen.
Example 1
Input: arr = [8, 3, 11, 5, 2], target = 10
trueExplanation: Two subsets add up to 10: [8, 2] and [3, 5, 2].
Example 2
Input: arr = [5, 9, 12], target = 8
falseExplanation: The subsets add up to 5, 9, 12, 14, 17, 21 and 26. None of them is 8.
Example 3
Input: arr = [6, 1, 4], target = 11
trueExplanation: Only the whole array works: 6 + 1 + 4 = 11. Every smaller subset adds up to 10 or less.
Check your understanding
Work out the answer for this input before you write any code.
Constraints
1 ≤ arr.length ≤ 10001 ≤ arr[i] ≤ 1041 ≤ target ≤ 104
Clarifying questions
What you would ask an interviewer before writing any code.