Skip to content

Commit a514119

Browse files
committed
Runtime: 130 ms (Top 82.58%) | Memory: 44.7 MB (Top 86.45%)
1 parent dd57a90 commit a514119

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
// Runtime: 130 ms (Top 82.58%) | Memory: 44.7 MB (Top 86.45%)
12
var numRollsToTarget = function(n, k, target) {
2-
if (n > target || n * k < target) return 0 //target is impossible to reach
3+
if (n > target || n * k < target) return 0 //target is impossible to reach
34
let arr = new Array(k).fill(1), depth = n //start the first layer of Pascal's N-ary Triangle.
45
while (depth > 1) { //more layers of Triangle to fill out
5-
tempArr = [] //next layer of triangle. not done in place as previous layer's array values are needed
6+
tempArr = [] //next layer of triangle. not done in place as previous layer's array values are needed
67
for (let i = 0; i < arr.length + k - 1 && i <= target - n; i++) { //looping is bounded by size of next layer AND how much data we actually need
78
let val = ((tempArr[i - 1] || 0) + (arr[i] || 0) - (arr[i - k] || 0)) % (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 values
89
tempArr.push(val)
@@ -12,4 +13,4 @@ var numRollsToTarget = function(n, k, target) {
1213
}
1314
let ans = arr[target - n] //answer will be in target - nth index
1415
return ans < 0 ? ans + 1000000007 : ans
15-
};
16+
};

0 commit comments

Comments
 (0)