‹ back to the board

Monday, October 26

Adeptalgorithms & data structures

The Ash-Choir of Mount Keldrith

Mount Keldrith sleeps only while it is sung to. Each night the monks of the Ash-Choir take turns chanting on the crater's rim. The volcano stirs during any moment when no monk is chanting. The Choir's abbess offers a bounty to whoever can name every silent span before the eruption-wardens post their watch.

Write silentSpans(chants, night).

  • chants is a list of pairs [start, end] of integers. Each pair is one monk's chant and covers the half-open span from start up to but not including end. Every pair satisfies 0 <= start < end <= night. The list may be empty and is in no particular order. Chants may overlap, nest inside one another, repeat exactly, or touch end-to-start.
  • night is a positive integer. The night is the half-open span from 0 up to night.

Return the silent spans as a list of pairs [start, end]. A silent span is a maximal half-open span within [0, night) that no chant covers.

  • The list is sorted by start, ascending.
  • Every span has start < end.
  • No two returned spans overlap or touch.

If every moment of the night is covered, return an empty list.

  • interval merging
  • Adept

examples

  1. case 1chants of [[2,5], [0,1], [4,7], [9,10]], night of 12[[1,2], [7,9], [10,12]]. The chants [2,5) and [4,7) overlap, so they leave no silence between them.
  2. case 2chants of [[0,3], [3,6]], night of 6[]. One chant ends at the very hour the next begins, so there is no silence.
  3. case 3no chants at all, night of 5[[0,5]]
reward
320 XP
signature
silentSpans
not yet run

TypeScript, run as ES2022 — types are stripped, not checked. If a JS engine would run it, so will we.

loading the editor…
  • overlapping and unsorted
  • touching chants leave no silence
  • no chants
  • nested chants
  • one chant covers the whole night
  • duplicates and out of order
  • silence only at the end
  • silence at both ends
  • short chant inside a long one ends earlier
The Ash-Choir of Mount Keldrith · bitwick