forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDice Roll Simulation.js
61 lines (48 loc) · 1.66 KB
/
Dice Roll Simulation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/** https://leetcode.com/problems/dice-roll-simulation/
* @param {number} n
* @param {number[]} rollMax
* @return {number}
*/
var dieSimulator = function(n, rollMax) {
// Memo
this.memo = new Map();
// Modulo
this.mod = (10 ** 9) + 7;
// Keep track of roll count for each die's face (1-6)
let currRoll = Array(6).fill(0);
return dp(n, rollMax, n, currRoll) % this.mod;
};
var dp = function(n, rollMax, nRemain, currRoll, previous = null) {
let key = `${nRemain}_${currRoll.toString()}`;
// Base case
if (nRemain === 0) {
return 1;
}
// Return from memo
if (this.memo.has(key) === true) {
return this.memo.get(key);
}
let count = 0;
// Try every face of the die, the face is the number on the die
// The face index in array is represented by `i`
// The face on the die (the number) is represented by `i + 1`
for (let i = 0; i < 6; i++) {
// Clone object we use for tracking the roll count for each die, by cloning we don't the next recursive won't change the value of current object
let cloneRoll = [...currRoll];
// Increment the count of current face
cloneRoll[i] += 1;
// This face of the die hit limit, ignore it
if (cloneRoll[i] > rollMax[i]) {
continue;
}
// This face of the die is different that previous one, since the limit is only for consecutive count, it means the previous face's count should be reset
if (previous != null && i + 1 != previous) {
cloneRoll[previous - 1] = 0;
}
// Count next recursive
count += dp(n, rollMax, nRemain - 1, cloneRoll, i + 1) % this.mod;
}
// Set memo
this.memo.set(key, count);
return count;
};