← 返回 apple 的题目列表Subset Sum with Space-Optimized Dynamic Programming
类型:online_judge
Problem: Subset Sum
Given an array arr of non-negative integers and a non-negative integer target, determine whether there exists a subset whose sum is exactly equal to target.
Implement the solution using Dynamic Programming with space optimized to O(target).
Input Format
n target
arr[0] arr[1] ... arr[n-1]
Output Format
Print:
true
if such a subset exists; otherwise print:
false
Constraints
1 <= n <= 200
0 <= target <= 10000
0 <= arr[i] <= target
Requirements
Use a one-dimensional DP array.
Time complexity should be O(n * target).
Space complexity should be O(target).
Example
Input:
5 9
3 34 4 12 5
Output:
true
Explanation: subset {4, 5} sums to 9.
Example
Input
5 9
3 34 4 12 5
Output
true