Tabulation (Bottom-Up DP) in C

Advanced
6 min read
Updated: Nov 2026
1 example

Bottom-Up with a Table

Tabulation solves the smallest subproblems first and fills a table with loops, with no recursion. Each cell depends only on cells already filled. It avoids stack depth limits and usually runs faster than memoization. In minimum coin change, dp[a] is the fewest coins for amount a, computed as min(dp[a - coin] + 1) over coins that fit.

Remember
dp[0] = 0;
for (a = 1; a <= amount; a++) {
    dp[a] = INF;
    for each coin c: if (c <= a && dp[a - c] + 1 < dp[a]) dp[a] = dp[a - c] + 1;
}

Cheat Sheet

Keep these facts handy while you work with Tabulation (Bottom-Up DP) in C in C.

TopicDetail
DirectionSmall to large
RecursionNone
TimeO(amount x coins)
SpaceO(amount)

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Two-Pointer Technique in C.

Example 1 — Minimum coins for 6 with {1, 3, 4}

The greedy answer (4 + 1 + 1) is not optimal.

C
#include <stdio.h>
#define INF 99

int main(void) {
    int coins[] = {1, 3, 4};
    int amount = 6, dp[7];

    dp[0] = 0;
    for (int a = 1; a <= amount; a++) {
        dp[a] = INF;
        for (int i = 0; i < 3; i++)
            if (coins[i] <= a && dp[a - coins[i]] + 1 < dp[a])
                dp[a] = dp[a - coins[i]] + 1;
    }

    printf("dp:");
    for (int a = 0; a <= amount; a++) printf(" %d", dp[a]);
    printf("\nminimum coins for %d: %d\n", amount, dp[amount]);
    return 0;
}

How It Works

  1. dp[3] and dp[4] are 1 because those are coin values.
  2. dp[6] uses dp[3] + 1 = 2, meaning 3 + 3.
  3. Greedy would pick 4 + 1 + 1 = 3 coins, so tabulation beats greedy here.

Edge Cases & Pitfalls

Check these before relying on Tabulation (Bottom-Up DP) in C in real programs.

inf

Overflow with INF

Adding 1 to a huge INF can overflow; use a moderate value or check first.

init

Uninitialized cells

Every cell must be set before it is read.

Key Takeaways

  • Fill small to large.
  • No recursion.
  • Cell = best of smaller cells.

One line: Tabulation solves the smallest subproblems first and fills a table with loops, with no recursion.

Frequently Asked Questions

A bottom-up approach that iteratively fills a table of subproblem answers starting from the base cases.
Tabulation avoids recursion overhead and stack limits; memoization only computes the states that are actually needed.
Continue with "Two-Pointer Technique in C" next. Find a pair in a sorted array and remove duplicates in place. The C Algorithm Techniques hub lists every topic in order.

Next: Two-Pointer Technique in C

Find a pair in a sorted array and remove duplicates in place.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful