1
var numRollsToTarget = function (n, k, target) {2
if (n > target || n * k < target) return 0; //target is impossible to reach3
let arr = new Array(k).fill(1),4
depth = n; //start the first layer of Pascal's N-ary Triangle.6
//more layers of Triangle to fill out7
tempArr = []; //next layer of triangle. not done in place as previous layer's array values are needed8
for (let i = 0; i < arr.length + k - 1 && i <= target - n; i++) {9
//looping is bounded by size of next layer AND how much data we actually need11
((tempArr[i - 1] || 0) + (arr[i] || 0) - (arr[i - k] || 0)) %12
1000000007; //current index value is the sum of K number of previous layer's values, once we hit K we add next and subtract last so we don't have to manually add all K values18
let ans = arr[target - n]; //answer will be in target - nth index19
return ans < 0 ? ans + 1000000007 : ans;