Dynamic Programming21 sections · 916 units
Open in Course

Assignment - State Design

Dp[mask] alone

dp[mask]dp[\text{mask}] = minimum cost when the first popcount(mask) elements of nums1 have been assigned to the nums2 elements indicated by mask.

Base case: dp[0]=0dp[0] = 0. No assignments made, zero cost. Transition: Let k=popcount(mask)k = \text{popcount}(\text{mask}). For each unset bit jj, try assigning nums1[kk] to nums2[jj]. dp[mask(1<<j)]=min(dp[mask(1<<j)],dp[mask]+nums1[k]nums2[j])dp[\text{mask} | (1 << j)] = \min(dp[\text{mask} | (1 << j)], dp[\text{mask}] + \text{nums1}[k] \oplus \text{nums2}[j])