Skip to content
CCC Python Course

Sorting with keys, and greedy

Stage
4
Module
M4.2
Lessons
1

In this module

  • Use sorted() with a key function to arrange data for greedy algorithms.
  • Solve ordering and selection problems where a greedy choice is optimal.
  • Recognize when sorting by one criterion then selecting solves the problem.
  • Handle ties and edge cases when the greedy choice is not unique.

Before this module

Lessons

  1. 1Sorting with keys and greedy

Practice

Try these on the judge. Each link opens the problem on DMOJ.

  1. 2016 S2
    Tandem Bicycle (opens on DMOJ in a new tab) DMOJ

    Sort two lists of times and pair them to get the fastest and the slowest combined result.

    Why DMOJ: A two-key greedy-after-sort problem, the same shape as this lesson's ratio sort.

  2. 2020 S1
    Surmising a Sprinter's Speed (opens on DMOJ in a new tab) DMOJ

    Scan and sort a set of measurements to work out a consistent value.

    Why DMOJ: A scan-then-sort problem in the same style as this lesson's key-based sorting.