Lessons 26–30 · About 60 min per lesson
Combined problems, proofs and review
Decide which tool handles each part of a problem and verify the combined solution.
Lesson breakdown
- Lesson 26
Choose states, a graph model or dynamic aggregation from constraints.
- Lesson 27
Combine sorting for rank compression with a Fenwick tree to count inversions.
- Lesson 28
Write a correctness argument for the key transition or update.
- Lesson 29
Design an exhaustive reference, random small inputs and important boundary cases.
- Lesson 30
Organise approaches, code, checks and costs for two problems.
Worked example
Count inversions in [3,1,2,1], using strictly greater left values.
How to solve it
Scanning left to right, count previously seen values that are strictly greater. Counts are 0, 1, 1 and 2, for 4. The equal 1s contribute no pair.
Assignment
Compare your inversion counter with nested loops. For the final assessment, derive the time bound for Time is Mooney and handle equal shortest paths in Shortcut.
Check your result
The example gives 4, equal values give 0, and four decreasing values give 6. For each past problem, submit code, complexity and a test that would catch a wrong approach.