Bitpath

Boss lesson · Unit 02

Coin change

Fewest coins with an infinity sentinel, then counting ways

13 exercisesabout 3 min

What you practise

  • Fewest coins with an infinity sentinel
  • Counting ways and why loop order matters

Try a question from this lesson

Pick one

Coins of value 1, 3 and 4, each usable any number of times. Taking the biggest coin that fits each time, how many coins make 6, and what is the fewest possible?

The ideas

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

Concept

Making change

You have coin values, each usable any number of times, and an amount to pay.

Question one: what is the fewest coins that add up to the amount exactly? If no combination works, the answer is -1.

Question two, later: how many different combinations make the amount?

With coins 1, 3 and 4, the amount 6 needs only two coins: 3 + 3.

Concept

Infinity means "not yet"

Start every cell at infinity: no way found yet. Only dp[0] is 0, because zero coins make zero.

Any real count beats infinity in a min. And infinity plus 1 is still infinity, so an unreachable amount never looks reachable. Whatever is still infinite at the end cannot be made.

INF = float("inf")
dp = [0, INF, INF, INF]
dp[3] = min(dp[3], dp[0] + 1)
print(dp[3], dp[2] == INF)
const INF = Infinity;
const dp = [0, INF, INF, INF];
dp[3] = Math.min(dp[3], dp[0] + 1);
console.log(dp[3], dp[2] === INF);
const INF = Infinity;
const dp = [0, INF, INF, INF];
dp[3] = Math.min(dp[3], dp[0] + 1);
console.log(dp[3], dp[2] === INF);

Python notefloat("inf") compares greater than every int. math.inf is the same value.

TypeScript noteInfinity has type number, so dp is still inferred as a plain number[].

Concept

Counting the ways

A different question fits the same table. Let ways[a] be the number of coin combinations that make a. There is one way to make 0: use nothing.

Process one coin at a time. For each amount, every way to make a - c becomes a way to make a by adding coin c.

def count_ways(coins, goal):
    ways = [1] + [0] * goal
    for c in coins:
        for a in range(c, goal + 1):
            ways[a] += ways[a - c]
    return ways[goal]
function countWays(coins, goal) {
  const ways = Array(goal + 1).fill(0);
  ways[0] = 1;
  for (const c of coins) {
    for (let a = c; a <= goal; a++) {
      ways[a] += ways[a - c];
    }
  }
  return ways[goal];
}
function countWays(
  coins: number[],
  goal: number,
): number {
  const ways = Array<number>(goal + 1)
    .fill(0);
  ways[0] = 1;
  for (const c of coins) {
    for (let a = c; a <= goal; a++) {
      ways[a] += ways[a - c];
    }
  }
  return ways[goal];
}

Python noteStarting the inner range at c replaces the if c <= a test: smaller amounts cannot use this coin.

TypeScript noteArray<number>(goal + 1) states the element type up front, so .fill(0) returns number[] and not any[].

9 more exercises in the app

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

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