Skip to content
We areTOP 1%
DSA · Competitive Programmer · Pattern 5 of 9

Advanced DP (Bitmask, Digit, Interval)

15 problems.
Your progress0%
1st Visit
0/15
2nd Visit
0/15
Revise
0/15

About this pattern

Some DP states do not fit a simple index. Bitmask DP encodes a subset of up to about 20 items in an integer, as in dp[mask][last] for ordering or assignment problems, usually costing O(2^n * n^2). Digit DP counts numbers in a range [L, R] with a digit property by walking the digits of the bound with a tight flag and computing f(R) - f(L - 1). Interval DP solves dp[l][r] for a contiguous segment by choosing a split point or the element handled last, typically in O(n^3). Signals: n <= 20 for bitmasks, bounds up to 10^18 with a digit condition, or merging and deleting adjacent blocks for intervals.

// Bitmask DP over orderings: dp[mask][last], n <= 18 items
long long bestOrder(int n, const vector<vector<long long>>& gain) {
    const long long NEG = LLONG_MIN / 4;
    vector<vector<long long>> dp(1 << n, vector<long long>(n, NEG));
    for (int i = 0; i < n; i++) dp[1 << i][i] = 0;
    long long best = NEG;
    for (int mask = 1; mask < (1 << n); mask++)
        for (int last = 0; last < n; last++) {
            if (dp[mask][last] == NEG) continue;
            best = max(best, dp[mask][last]);
            for (int nxt = 0; nxt < n; nxt++) {
                if (mask >> nxt & 1) continue;
                long long& to = dp[mask | 1 << nxt][nxt];
                to = max(to, dp[mask][last] + gain[last][nxt]);
            }
        }
    return best;
}

Problems

Advanced DP (Bitmask, Digit, Interval) questions
#ProblemPlatformDifficultyOpen1st1st Visit2nd2nd VisitRevRevise
1Kefa and DishesCodeforcesHard 1800Solve Kefa and Dishes on Codeforces (opens in a new tab)
2Minimum Cost to Cut a StickLeetCodeHard Solve Minimum Cost to Cut a Stick on LeetCode (opens in a new tab)
3Shortest Path Visiting All NodesLeetCodeHard Solve Shortest Path Visiting All Nodes on LeetCode (opens in a new tab)
4Burst BalloonsLeetCodeHard Solve Burst Balloons on LeetCode (opens in a new tab)
5ZumaCodeforcesHard 1900Solve Zuma on Codeforces (opens in a new tab)
6FishCodeforcesHard 1900Solve Fish on Codeforces (opens in a new tab)
7Classy NumbersCodeforcesHard 1900Solve Classy Numbers on Codeforces (opens in a new tab)
8Number of Digit OneLeetCodeHard Solve Number of Digit One on LeetCode (opens in a new tab)
9Find the Shortest SuperstringLeetCodeHard Solve Find the Shortest Superstring on LeetCode (opens in a new tab)
10Clear the StringCodeforcesHard 2000Solve Clear the String on Codeforces (opens in a new tab)
11Square SubsetsCodeforcesHard 2000Solve Square Subsets on Codeforces (opens in a new tab)
12Array ShrinkingCodeforcesHard 2100Solve Array Shrinking on Codeforces (opens in a new tab)
13Compatible NumbersCodeforcesHard 2200Solve Compatible Numbers on Codeforces (opens in a new tab)
14Magic NumbersCodeforcesHard 2200Solve Magic Numbers on Codeforces (opens in a new tab)
15Segment SumCodeforcesHard 2300Solve Segment Sum on Codeforces (opens in a new tab)

Sign in to track your progress

Your checkboxes, streak and activity heatmap are saved to your account and sync across devices. It is free.
Sign in with email

New here? Create an account