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).
chantsis a list of pairs[start, end]of integers. Each pair is one monk's chant and covers the half-open span fromstartup to but not includingend. Every pair satisfies0 <= 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.nightis a positive integer. The night is the half-open span from0up tonight.
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.
examples
- case 1
chants 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. - case 2
chants of [[0,3], [3,6]], night of 6[]. One chant ends at the very hour the next begins, so there is no silence. - case 3
no chants at all, night of 5[[0,5]]
- reward
- 320 XP
- signature
silentSpans
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