1772. Dice Total Set

Easy1.5 points estimatedEnumerationHash TableSorting

A board game uses several custom dice. Die i is described by the list dice[i] of the values printed on its faces; faces of one die may repeat. Every die is rolled exactly once and any face of each die can come up.

Return all distinct totals that the sum of the rolled faces can take, as an array sorted in ascending order. Each total appears once, even if many different face combinations produce it. Enumerate the combinations of faces (there are at most 6^5 = 7776 of them), put each total into a set, and finally sort the set. With d dice and f faces per die this is O(f^d) time in the worst case and O(number of distinct totals) space, which is tiny under the limits below.

Example 1

Input:
dice = [[1,2],[10,20]]
Output:
[11,12,21,22]
Explanation:

The four combinations give 11, 21, 12 and 22.

Example 2

Input:
dice = [[1,2,3,4,5,6]]
Output:
[1,2,3,4,5,6]
Explanation:

One ordinary die can show any total from 1 to 6.

Example 3

Input:
dice = [[1,1],[2,2]]
Output:
[3]
Explanation:

Repeated faces collapse to a single total of 3.

Constraints

1 ≤ dice.length ≤ 5; 1 ≤ dice[i].length ≤ 6; 1 ≤ dice[i][j] ≤ 100

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Time per case
Python 4,000 msC++ 1,000 msJava 2,000 msJavaScript 2,000 msTypeScript 2,000 ms

Expected complexity

Time
O(f^d)
Space
O(S)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…