Skip to content

prev1 / prev2 [terminology]

Terminology

Usage

  • Use for dynamic programming states one and two positions back.
  • The suffix should describe distance from the current state:
    • prev1: one position back
    • prev2: two positions back
  • Update them so their meaning remains stable after every iteration.
  • Prefer more semantic names when the recurrence does not naturally use relative positions.
prev2, prev1 = 1, 1

for i in range(1, len(s)):
    cur = prev1
    prev2, prev1 = prev1, cur