TRACK B · ALGORITHMmedium•NOVEMBER 2026•7 MIN
Dynamic Programming: Bottom-Up Subproblem Tables
Compute globally optimal answers from strictly smaller solved states whenever greedy choices can trap you.
dynamic programmingoptimizationcoin-changeknapsack
The core idea
Define a clean state dp[t] = optimal answer for budget t. Because every shard size is positive, dp[t - sz] is already solved before you compute dp[t].
Watch it run
Step through the trace below (or turn on Predict Mode) to watch the invariant hold at every step.
LOADING DYNAMIC PROGRAMMING TABLE SIM...
What to say out loud
Canonical Python template
def min_shards(sizes: list[int], target: int) -> int:
dp = [float("inf")] * (target + 1)
dp[0] = 0
for t in range(1, target + 1):
for sz in sizes:
if t >= sz:
dp[t] = min(dp[t], dp[t - sz] + 1)
return int(dp[target]) if dp[target] < float("inf") else -1