Combinatorics · Medium
A scheduler estimates how many different value sequences can be formed from a small batch. All entries must appear, but equal values are indistinguishable in the output sequence. Enumerating every index permutation would overcount duplicates and waste time.
Count distinct value orderings that use every input occurrence exactly once.
Input
Call
- Implement solve(input) and return the result. The provided adapter handles JSON input and output.
Input shape
{ nums: integer[] }
Fields
- nums is an array of signed integer labels. Repeated labels must retain their multiplicities.
Output
Return value
- Return the number of distinct value sequences, not index permutations or the sequences themselves.
- The empty batch has one empty ordering. If all values are equal, return one.
Constraints
- 0 <= nums.length <= 18; -1000000000 <= nums[i] <= 1000000000.
- The final answer is exact and at most 18!, within a safe JSON integer. Use integer arithmetic when dividing factorials.
Examples
Example 1
{
"nums": [
1,
1,
2
]
}Returns
3Example 2
{
"nums": [
4,
4,
4
]
}Returns
1Required complexity
O(n) frequency updates and factorial operations with O(n) auxiliary space