NISHIO Hirokazu[Translate]
累積和しながらDP

DPなどの最中に累積和の動的生成することがある
python
def accum_generation(N): """ >>> accum_generation(10) [1, 0, 1, 1, 1, 2, 2, 3, 4, 5] """ value = [0] * (N + 10) accum = [0] * (N + 10) value[0] = 1 accum[0] = 1 for pos in range(1, N): ret = (accum[pos - 2] - accum[pos - 4]) value[pos] = ret accum[pos] = accum[pos - 1] + ret return value[:N]

"Engineer's way of creating knowledge" the English version of my book is now available on [Engineer's way of creating knowledge]

(C)NISHIO Hirokazu / Converted from [Scrapbox] at [Edit]