The Thornback Lullaby of Orvask Hollow
In the Orvask Hollow sleeps the Thornback Behemoth, and the road to the Hollow's salt-springs runs past its nose. The Lyreward Guild keeps it asleep by playing a lullaby while caravans pass, but the Behemoth stirs at a careless tune. The Guild will pay for a reckoning that reads a written lullaby and says where, if anywhere, the beast would wake.
Write wakingNote(notes, leap).
notesis a list of integers, the pitches of the lullaby in the order they are played. Pitches may be negative. The list may be empty.leapis a non-negative integer.
The Behemoth wakes on the note at index i (counting from 0) if either of these is true:
1. i >= 1 and the absolute difference between notes[i] and notes[i - 1] is strictly greater than leap. 2. i >= 2 and notes[i], notes[i - 1] and notes[i - 2] are all equal.
Return the smallest index at which the Behemoth wakes, as an integer. If it wakes on no note, return -1.
examples
- case 1
notes of [5, 7, 6, 9, 8], leap of 23 (the step from 6 up to 9 is a gap of 3, more than 2) - case 2
notes of [1, 3, 5, 7], leap of 2-1 (every gap is exactly 2, which is allowed) - case 3
notes of [4, 4, 4, 5], leap of 32 (the third 4 in a row wakes it)
- reward
- 220 XP
- signature
wakingNote
TypeScript, run as ES2022 — types are stripped, not checked. If a JS engine would run it, so will we.
- wide gap mid-song
- gaps exactly at leap are safe
- three repeated notes
- empty lullaby
- single note with zero leap
- pairs repeat but never three
- gap wakes before a later triple
- zero leap with repeats
- boundary gap then triple
- negative pitches
- zero leap any change wakes