Problem 521674 · easy · Level 05 Advanced Algorithms & Graphs

Dice Roll Sequences With a Given Sum

backtracking · sequences · pruning

A board game rolls dice dice one after another; each die shows a number from 1 to faces. Return every ordered sequence of rolls whose total is exactly target. [1, 4] and [4, 1] are different sequences. The sequences may be returned in any order.

Examples

Input:  dice = 2, faces = 4, target = 5
Output: [[1, 4], [2, 3], [3, 2], [4, 1]]

Input:  dice = 3, faces = 6, target = 2
Output: []
Explanation: three dice always show at least 3.

Input:  dice = 1, faces = 6, target = 6
Output: [[6]]

Constraints

  • 0 <= dice <= 5, 1 <= faces <= 8, 0 <= target <= 40

Goals

  • Generate ordered sequences where every level has the same set of choices
  • Prune a branch when the remaining sum is out of reach
Starting Python…