‹ back to the board

Friday, September 18

Adeptalgorithms & data structures

The Lanterns of Dusk Lane

The lamplighter of bitwick keeps Dusk Lane, where the lanterns stand in a single row from the gate to the well. Each evening she records how brightly each one burns, and the Lamplighters' Guild pays only for steady work: a run of neighbouring lanterns counts as steady when the brightest and the dimmest lantern in that run differ by no more than the allowance the Guild set for the night.

You are given:

  • lamps — a list of integers, the brightness of each lantern in order along the lane. Values may be negative. The list may be empty.
  • spread — a non-negative integer, the Guild's allowance for the night.

Return an integer: the number of lanterns in the longest contiguous run of lamps in which (maximum brightness − minimum brightness) is at most spread. A run of a single lantern always qualifies, since its maximum and minimum are the same. If lamps is empty, return 0.

Write longestSteadyStretch(lamps, spread).

  • two pointers and sliding windows
  • Adept

examples

  1. case 1lamps of [1, 5, 6, 7, 2, 3], spread of 23 — the run 5, 6, 7 spans 2 brightness and holds three lanterns; the run 2, 3 holds only two
  2. case 2lamps of [9, 1, 9], spread of 01 — no two neighbouring lanterns match, so a lone lantern is the longest steady run
  3. case 3lamps of [], spread of 30 — the lane is dark
reward
320 XP
signature
longestSteadyStretch
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…
  • the guild's sample night
  • a flat plateau at zero allowance
  • the dark lane
  • a single lantern
  • no two neighbours agree
  • a fading start and a steady middle
  • lanterns below the frost line
  • allowance covers the whole lane
  • longest identical run
  • peak hides between two bands
The Lanterns of Dusk Lane · bitwick