Advanced Dynamic Programming: Mixed Problems, State Design and a GATE-Style Example

Learn how sorting, binary search and DP work together in weighted interval scheduling, then recover the chosen jobs and test the method on a changed input.

KnowledgeGate Team

Exam prep & CS education

Updated 27 Nov 20265 min read
Advanced Dynamic Programming: Mixed Problems, State Design and a GATE-Style Example

You can fill a familiar knapsack table, yet freeze when a problem first asks you to sort, run binary search and then apply DP. The way out is not to memorise a stranger recurrence. It is to define exactly what each subproblem retains, prove the transition, order the evaluation and recover the selected solution.

What makes a dynamic programming problem advanced or mixed

Every sound DP solution has four parts: a state with one precise meaning, a recurrence covering every valid final choice, correct base cases, and an evaluation order in which every dependency is already known. For weighted interval scheduling, define best[i] as the maximum profit obtainable from the first i jobs sorted by finish time.

A mixed problem uses DP as one layer. Here, sorting creates a useful order, binary search finds the last compatible job, and DP decides whether to take or skip the current job. Forcing all three responsibilities into one recurrence only hides the reasoning.

If overlapping subproblems, memoisation and tabulation are not yet comfortable, revisit Dynamic Programming Explained. The GATE category gives the broader preparation route.

Design the state before writing the recurrence

Test a proposed state with three questions:

  1. What smaller input is left?

  2. What past information can still change a future decision?

  3. Can two histories with the same state always share the same optimal continuation?

If the third answer is no, the state has lost necessary information.

For interval scheduling, best[i] over jobs in arbitrary input order is not useful because compatible predecessors need not lie in the prefix. Sort complete job records by finishing time, and every predecessor of job i now appears before it.

Let p(i) be the largest index j < i for which finish[j] <= start[i]. Use 0 if no such job exists. Then:

best[0] = 0

best[i] = max(best[i-1], profit[i] + best[p(i)])

The first branch skips job i. The second takes it and combines its profit with the best prefix that cannot overlap it. These cases are exhaustive because an optimal solution either contains job i or it does not, and mutually exclusive because it cannot do both.

Worked example: weighted interval scheduling from input to answer

Use these jobs in (start, finish, profit) form: J1=(1,3,5), J2=(2,5,6), J3=(4,6,5), J4=(6,7,4), J5=(5,8,11), J6=(7,9,2). They are already sorted by non-decreasing finish time. Touching endpoints are compatible because the rule is finish <= start.

The predecessor indices are p(1)=0, p(2)=0, p(3)=1, p(4)=3, p(5)=2, and p(6)=4. For example, J4 can follow J3 because 6 <= 6. For J5, J2 is the latest valid predecessor because 5 <= 5, while J3 finishes at 6.

Now fill both branches, not just the winning value:

i

Job

p(i)

Skip

Take

best[i]

0

None

None

None

None

0

1

J1

0

0

5 + 0 = 5

5

2

J2

0

5

6 + 0 = 6

6

3

J3

1

6

5 + 5 = 10

10

4

J4

3

10

4 + 10 = 14

14

5

J5

2

14

11 + 6 = 17

17

6

J6

4

17

2 + 14 = 16

17

The optimum profit is 17. It means the best compatible subset among all six jobs earns total profit 17. For structured subject-wise study and problem practice, continue with GATE Guidance by Sanchit Sir.

Timeline of six weighted jobs from time 1 to 9, including J2 from 2 to 5, J5 from 5 to 8, and optimum profit 17.

Recover the chosen jobs and account for complexity

Trace backwards from i=6. Since best[6]=best[5]=17, skip J6. At i=5, taking J5 gives 11+best[2]=17, beating best[4]=14, so select J5 and jump to i=2. Taking J2 gives 6+best[0]=6, beating best[1]=5, so select it and stop at 0. Reverse the collected list to get [J2, J5], total profit 17.

If take and skip are equal, either preserves the optimal value. An implementation still needs a deterministic tie rule if it must prefer fewer jobs, earlier jobs or another secondary objective.

Sorting costs O(n log n). Finding every p(i) by binary search also costs O(n log n), while the table and traceback cost O(n). Total time is O(n log n) with O(n) extra space. Scanning backwards separately for every predecessor would raise that discovery step to O(n^2).

DP table and traceback for weighted interval scheduling, with best value 17 and traceback selecting J5 and J2.

Recognise the DP family and the non-DP ingredient

Clue

Likely state

Evaluation order

Extra technique

Sequence or prefix

dp[i]

Left to right

Prefix data or search

Interval

dp[l][r]

Increasing interval length

Try split points

Subset with endpoint

dp[mask][last]

Increasing masks

Bit operations

Weighted scheduling

best[i]

Finish-time order

Sorting and binary search

For a small interval-DP example, let matrices be A:10x30, B:30x5, and C:5x60. The order (AB)C costs 10*30*5 + 10*5*60 = 1,500 + 3,000 = 4,500 scalar multiplications. The order A(BC) costs 30*5*60 + 10*30*60 = 9,000 + 18,000 = 27,000. A state such as dp[l][r] must therefore try split points rather than assume one fixed grouping.

Sorting, binary search or graph traversal may prepare the dependency structure without replacing DP. Sorting Algorithms: Comparison and Complexity is useful follow-on reading for the ordering layer. The Graph Algorithms guide covers another common way to expose dependencies.

Common traps and their smallest counterexamples

  • Using a greedy rule. Highest individual profit chooses A=(1,4,8), but compatible jobs B=(1,2,5) and C=(2,4,5) give 10. Weighted interval scheduling needs DP unless a greedy-choice property is proved.

  • Treating touching intervals as conflicting. J3=(4,6,5) and J4=(6,7,4) are compatible under 6 <= 6. Replacing <= with < changes p(4) and may change the answer. Copy the inequality into the search and tests.

  • Searching before sorting. Keep each job as one record, sort the records together, and verify p(i) < i. Never separate profits from their jobs while sorting.

  • Returning only the value. If selected jobs are required, store decisions or replay the same take and skip expressions. One-dimensional memory optimisation is unsuitable for reconstruction unless enough decision data remains.

How exams test advanced DP and mixed problems

Typical tasks ask you to choose the minimal valid state, complete a recurrence, calculate table entries, find predecessor indices, compare complexities, reconstruct an optimal set or reject an invalid greedy rule.

Try a timed check on the running data: find p(5), the take value for J5, and best[5]. The answers are 2, 11+best[2]=17, and 17.

Now change only J5's profit from 11 to 7. Then best[5]=max(14,7+6)=max(14,13)=14, and best[6]=max(14,2+14)=16. Traceback selects [J1,J3,J4,J6], worth 5+5+4+2=16.

Use the GATE Test Series for test-format practice. Confirm current dates, marks, topic weightage and question counts on the official GATE 2027 website.

The short version and the next step

Before accepting a DP solution, run five checks: define the state in one sentence, verify that equal states have equal futures, write every take, skip or split case, order the dependencies, and reconstruct from the same recurrence.

Redo the six-job table with J5 changed to profit 7. Confirm the new value 16 and selected set [J1,J3,J4,J6]. If the recurrence still feels abrupt, revisit the foundational DP article linked above. Then use the GATE category for the wider route, or the guidance course when you want structured subject-wise study.