Here is a complete, interactive Tailwind CSS page that visualizes Dynamic Programming. It includes the **1D Sequential Array** (recreated from your image with animated arrows), a **2D Array Grid** (Unique Paths problem), and a **Recursive Top-Down Tree**. ```html
Dynamic Programming
Explore how dynamic programming optimizes algorithms using top-down recursion, 1D arrays, and 2D matrices.
01 — Bottom-Up
Recreated from your image. The array is filled sequentially from left to right. Each cell depends on the two previous cells.
fib[0] = 0;
fib[1] = 1;
for (i = 2; i <= n; i++) {
fib[i] = fib[i-1] + fib[i-2];
}
return fib[n];
02 — Bottom-Up
Moving from top-left to bottom-right, each cell sums the paths from the cell above it and the cell to its left.
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
if (i==0 || j==0)
dp[i][j] = 1; // base case
else
dp[i][j] = dp[i-1][j] + dp[i][j-1];
}
}
03 — Top-Down
Instead of filling an array, we start from the top and recurse down. Repeated branches are served instantly from the cache.
// Memoized Recursive
function fib(n, memo = {}) {
if (n <= 1) return n;
if (memo[n]) return memo[n]; // cache hit
return memo[n] = fib(n-1, memo) + fib(n-2, memo);
}