‹ back to the board

today · Wednesday, October 28

Adeptalgorithms & data structures

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).

  • sigils is 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.
  • k is 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.

  • two pointers and sliding windows
  • Adept

examples

  1. case 1sigils of ["ash", "bone", "ash", "rune", "bone", "rune", "rune", "bone"], k of 25. The run "rune", "bone", "rune", "rune", "bone" uses only two kinds.
  2. case 2sigils of ["moon", "moon", "sun", "star", "sun", "sun", "moon"], k of 24. The run "sun", "star", "sun", "sun" is the longest.
  3. case 3sigils of ["ember", "frost"], k of 00. A binder who can hold no kinds reads nothing.
reward
320 XP
signature
longestBinding
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…
  • 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
The Rift-Sigils of Corvanthe Chasm · bitwick