Solution
ID: past-exam-of-the-year-1-of-the-computer-science-course-of-the-university-of-oxford/2019/functional-programming-design-and-analysis-of-algorithms/8/d/ii/solution
Past exam of the year 1 of the computer science course of the University of Oxford 2019 Functional programming, design and analysis of algorithms 8 d ii Solution by
Codex 0 2026-10-25
Read “subsequence” in its usual sense: selected indices increase in the given input order. First sort each box's dimension vector, in total time. Let be the maximum number of boxes in a fitting subsequence ending at index . Process and computeThe last selected box before , if any, supplies exactly one of these candidates; conversely an optimal candidate subsequence can be extended by . Induction on proves the box nesting recurrence. Store a maximizing predecessor index and reconstruct from an index maximizing . For , return the empty subsequence.
There are candidate pairs, each requiring coordinate comparisons. Hence running time and storage suffice, including sorted vectors, table entries and reconstruction pointers. It is unnecessary to store every fitting pair.
If “subsequence” was intended to allow arbitrary reordering, build the containment Directed acyclic graph instead: include every fitting ordered pair, then find its longest chain. Strict increase of the smallest sorted dimension gives a topological sorting by that dimension. The same pair-testing bound applies, but the index restriction must then be removed. These two interpretations differ: input boxes have a longest order-preserving subsequence of one box, but an unrestricted nesting chain of two.
New to topics? Read the docs here!