Problema
A standard die produces an integer from 1 through 6. Count all nonempty ordered sequences of outcomes whose sum is target. Different orders count separately. Return the count modulo 1,000,000,007.
Implement dice_combinations(target).
Input
target: The required sum of all rolls.
Output
Return the number of sequences modulo 1,000,000,007.
Vincoli
1 ≤ target ≤ 1,000,000. Taking a count modulo 1,000,000,007 means returning its remainder after division by that number.
Esempi
Input:
target = 2
Output atteso:
expected = 2
Input:
long long target = 2;
Output atteso:
long long expected = 2;
Input:
long target = 2;
Output atteso:
long expected = 2;
Spiegazione
The two sequences are [2] and [1, 1].
Input:
target = 4
Output atteso:
expected = 8
Input:
long long target = 4;
Output atteso:
long long expected = 8;
Input:
long target = 4;
Output atteso:
long expected = 8;
Spiegazione
The sequences are [4], [1,3], [3,1], [2,2], [1,1,2], [1,2,1], [2,1,1], and [1,1,1,1].
Come affrontarlo
Prova prima da solo, poi apri un suggerimento alla volta.
Suggerimento 1
Think of smaller sums as building blocks for the target.
Suggerimento 2
Use a running count for each sum based on the previous six sums.
Suggerimento 3
Be careful to apply the modulo at every addition and handle sums below 1 separately.
Complessità attesa
O(target) time, O(target) extra space
Riferimenti
Questa sfida è adattata per CodeUtility. Segui la traccia e i vincoli sopra.