AcWing 80. 骰子的点数
原题链接
简单
作者:
zc2077
,
2020-07-13 00:08:53
,
所有人可见
,
阅读 517
dp
f[n]
是投n
次骰子得到的点数序列,有
- $f[n] = (a_{n}, a_{n+1},\dots, a_{6 * n})$;
- $f[n-1] = (b_{n - 1}, b_{n},\dots, b_{6 * (n - 1)})$
- 得到
a_{n} = b_{n - 1}(n=1)
,a_{n + 1} = b_{n-1}(n=2) + b_{n}(n=1)
....其中(n=1)代表投第n次时骰子数值为1,因此投第n
次的点数是上一次前最多6个数的和。
class Solution(object):
def numberOfDice(self, n):
"""
:type n: int
:rtype: List[int]
"""
init = [1, 1, 1, 1, 1, 1]
for _ in range(2, n + 1):
res, window, count = [], [], 0
for num in init:
if window and len(window) == 6:
count -= window.pop(0)
window.append(num)
count += num
res.append(count)
while window:
count -= window.pop(0)
if count:
res.append(count)
init = res
return init