The Rift-Sigils of Corvanthe Chasm
Along the lip of Corvanthe Chasm runs a single line of carved sigils, older than any road in the province. The Order of the Grey Lectern posts a bounty for a binder who can read the longest unbroken stretch of them in one sitting. A binder's mind can hold only so many *kinds* of sigil at once. A kind may appear any number of times within the stretch without costing anything extra.
Write longestBinding(sigils, k).
sigilsis a list of strings, in order along the chasm wall. Each string names the kind of one sigil. Two sigils are the same kind exactly when their strings are equal, and the comparison is case-sensitive.kis a non-negative integer: the most distinct kinds the binder can hold.
Return an integer: the length of the longest contiguous run of sigils (consecutive elements, in their given order) that contains at most k distinct strings. If no non-empty run qualifies, return 0. This includes the case where sigils is empty or k is 0.
examples
- case 1
sigils of ["ash", "bone", "ash", "rune", "bone", "rune", "rune", "bone"], k of 25. The run "rune", "bone", "rune", "rune", "bone" uses only two kinds. - case 2
sigils of ["moon", "moon", "sun", "star", "sun", "sun", "moon"], k of 24. The run "sun", "star", "sun", "sun" is the longest. - case 3
sigils of ["ember", "frost"], k of 00. A binder who can hold no kinds reads nothing.
- reward
- 320 XP
- signature
longestBinding
TypeScript, run as ES2022 — types are stripped, not checked. If a JS engine would run it, so will we.
- two kinds, later stretch wins
- middle stretch wins
- k covers every kind
- empty wall
- k of zero
- single sigil
- alternating with k of one
- all one kind
- case-sensitive kinds
- k exceeds kinds present