Skip to content
    Algoria

    ALGORITHMIC TECHNIQUES · 11 MIN

    Recursion, Greedy & Dynamic Programming

    Three ways to attack a problem: break it down, grab the best choice now, or reuse sub-answers.

    BYTE explains

    • Recursion: solve a smaller copy of the problem plus a base case.
    • Greedy: take the locally best option — works only when that is provably safe (e.g. coin systems like 1, 2, 5, 10).
    • Dynamic programming: store answers to overlapping sub-problems so each is solved once.

    Real world: Paying with the fewest notes (greedy) or planning the cheapest train route (DP).

    Code

    def climb(n):
        a, b = 1, 1
        for _ in range(n):
            a, b = b, a + b
        return a
    def coins(amount, cs=(10, 5, 2, 1)):
        out = []
        for c in cs:
            while amount >= c:
                amount -= c; out.append(c)
        return out
    print(climb(5), coins(18))
    Output ›
    8 [10, 5, 2, 1]
    Try it in the online compiler →

    Checkpoint quiz

    DP helps most when sub-problems…