Bitpath

Lesson 1 · Unit 02

From memo to table

A dp array filled smallest first, then two rolling variables

12 exercisesabout 3 min

What you practise

  • A dp array
  • Rolling variables for O(1) space

Try a question from this lesson

Pick one

ways(5) is called once. In which order are the answers stored in the memo?

memo = {}

def ways(n):
    if n <= 1:
        return 1
    if n not in memo:
        a, b = ways(n - 1), ways(n - 2)
        memo[n] = a + b      # stored
    return memo[n]

ways(5)
const memo = new Map();

function ways(n) {
  if (n <= 1) return 1;
  if (!memo.has(n)) {
    const a = ways(n - 1);
    const b = ways(n - 2);
    memo.set(n, a + b);   // stored
  }
  return memo.get(n);
}

ways(5);
const memo = new Map<number, number>();

function ways(n: number): number {
  if (n <= 1) return 1;
  if (!memo.has(n)) {
    const a = ways(n - 1);
    const b = ways(n - 2);
    memo.set(n, a + b);   // stored
  }
  return memo.get(n)!;
}

ways(5);

The ideas

Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.

Concept

Bottom-up: fill a table

Tabulation skips the recursion. Make an array dp where dp[i] is the answer for state i. Write in the base cases, then loop upward so every cell a step needs is already filled.

Here dp[i] is the number of ways to climb i stairs with moves of 1 or 2.

def ways(n):
    dp = [1] * (n + 1)
    for i in range(2, n + 1):
        dp[i] = dp[i - 1] + dp[i - 2]
    return dp[n]
function ways(n) {
  const dp = Array(n + 1).fill(1);
  for (let i = 2; i <= n; i++) {
    dp[i] = dp[i - 1] + dp[i - 2];
  }
  return dp[n];
}
function ways(n: number): number {
  const dp = Array<number>(n + 1).fill(1);
  for (let i = 2; i <= n; i++) {
    dp[i] = dp[i - 1] + dp[i - 2];
  }
  return dp[n];
}

Python note[1] * (n + 1) fills cells 0 and 1 with their base case and gives the loop slots to write into.

TypeScript noteArray<number>(n + 1) states the element type. Plain Array(n + 1).fill(1) would be typed any[], and mistakes in the loop would go unchecked.

Concept

Keep only what you need

Each cell reads just the two cells before it. So drop the array and carry two variables forward: prev and cur.

Same O(n) time, but O(1) space instead of O(n). The two must be updated together: the new cur needs the old prev.

def ways(n):
    prev, cur = 1, 1
    for _ in range(2, n + 1):
        prev, cur = cur, prev + cur
    return cur
function ways(n) {
  let [prev, cur] = [1, 1];
  for (let i = 2; i <= n; i++) {
    [prev, cur] = [cur, prev + cur];
  }
  return cur;
}
function ways(n: number): number {
  let [prev, cur] = [1, 1];
  for (let i = 2; i <= n; i++) {
    [prev, cur] = [cur, prev + cur];
  }
  return cur;
}

Python noteTuple assignment evaluates the whole right side first, so prev + cur still uses the old prev.

JavaScript note[prev, cur] = [cur, prev + cur] builds the right-hand array first, then unpacks it. The line before it must end with a semicolon, or JS reads the [ as an index.

TypeScript notelet [prev, cur] = [1, 1] infers number for both names from the array literal.

9 more exercises in the app

  • 2× type the answer
  • 1× fill the blanks
  • 3× multiple choice
  • 1× match the pairs
  • 1× true or false
  • 1× order the lines

Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.

One email when Bitpath is ready to try. Nothing else. Privacy