Dynamic programming · Medium
A batch contains indivisible non-negative workload amounts. A planner wants to know whether the entries can be assigned to two groups with the same total. Every entry must belong to one group, and identical amounts at different indexes remain separate entries.
Return whether all entries can be partitioned into two subsets with exactly equal sums.
Input
Call
- Implement solve(input) and return the result. The provided adapter handles JSON input and output.
Input shape
{ nums: integer[] }
Fields
- nums contains non-negative integers. Every occurrence is usable exactly once.
Output
Return value
- Return a boolean, not the groups themselves. Either group may be empty.
- An empty batch returns true. An odd total cannot be split equally.
Constraints
- 0 <= nums.length <= 200; 0 <= nums[i] <= 20000; sum(nums) <= 20000.
Examples
Example 1
{
"nums": [
1,
5,
11,
5
]
}Returns
trueExample 2
{
"nums": [
1,
2,
5
]
}Returns
falseRequired complexity
O(n × S) time and O(S) auxiliary space where S is half the total