Skip to content
CodeItRaw
Path

Problems that get easy once sorted: counting sort, the middle element, neighbour gaps, intervals and buckets.

Key ideas

  • Sorting puts "close" things side by side: nearest pairs, overlaps, the median.
  • sorted(xs, key=...) and tuple keys: the first criterion, then the second on ties.
  • With a small range of values, counting (counting sort) beats sorting.

Pattern

order = sorted(items, key=lambda it: (it[0], -it[1]))
for prev, cur in zip(order, order[1:]):
    ...

Finish these first:Two Pointers & Sliding WindowRecursion & Divide and Conquer

Lessons

Each lesson explains one idea from zero and ends with a few tasks. Lessons open in order; the explanation can be read at any time.

  1. 01

    sorted, keys and counting

    0/2 tasks

  2. 02

    How sorting works and what it costs

    0/2 tasks

  3. 03

    Sort, then look at the neighbours

    0/3 tasks

  4. 04

    After sorting the answer falls in a known place

    0/3 tasks

  5. 05

    Putting intervals in order of their start

    0/3 tasks

Boss

  • 06

    Maximum Gap

    Function

  • The boss opens when every lesson of the topic is finished. Solve it too and the topic is complete, and the topics after it open.