tinycodinglm / math /math-0157.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 16)
d517dc9 verified
|
Raw History Blame
1.35 kB
opencode trace
System You are an expert mathematician and programmer.

You are an expert mathematician and programmer.

User

Explain DP math foundations

Assistant

Optimal substructure: optimal solution contains optimal solutions to subproblems.
Overlapping subproblems: same subproblems solved repeatedly, memoize.

Knapsack: dp[i][w] = max value using items 1..i with capacity w. O(nW). Space optimize to O(W).
dp[j] = max(dp[j], dp[j-w_i]+v_i) iterating j backward (0-1) or forward (unbounded).

Longest common subsequence: dp[i][j] = dp[i-1][j-1]+1 if s[i]==t[j], else max(dp[i-1][j],dp[i][j-1]).
Edit distance: dp[i][j] = min operations to transform s[1..i] to t[1..j]. Insert/delete/replace.

Partition problems: dp[sum] = true if subset sums to total/2.
Bitmask DP: dp[mask] for subset {0..n-1} represented by bitmask. TSP: O(n*2^n).

Matrix chain multiplication: dp[i][j] = min cost to multiply matrices i..j. O(n^3).
Optimal BST: dp[i][j] = min cost tree for keys i..j with probabilities.

Interval DP: dp[i][j] = best for range [i,j]. Merge: dp[i][j] = min(dp[i][k]+dp[k+1][j]) for split k.