‹ back to the board

Thursday, October 8

Adeptalgorithms & data structures

The Totem Road of the Quillfang Steppe

Commission of the Quillfang Shamans' Lodge

Across the Quillfang Steppe runs the Totem Road: a single line of carved spirit-totems standing one after another. Each totem holds a measure of spirit-power, always a positive whole number. A war-band riding out against the Howling Things needs a ward, and a ward is raised by binding a contiguous run of totems with one cord. The ward holds if the total power of the bound totems is at least the strength required. A longer cord takes longer to tie, so the Lodge wants the shortest run that will do.

Write shortestBinding(powers, target).

  • powers is a list of positive integers (each at least 1), the power of each totem in road order. It may be empty.
  • target is a positive integer, the strength the ward needs.

Return an integer: the smallest number of consecutive totems whose powers sum to a value greater than or equal to target. If no contiguous run reaches target (including when powers is empty), return 0.

  • two pointers and sliding windows
  • Adept

examples

  1. case 1powers of [2, 3, 1, 2, 4, 3], target of 72 (the run 4, 3 sums to 7, which counts)
  2. case 2powers of [1, 2, 3, 4, 5], target of 155 (only the whole road reaches 15)
  3. case 3powers of [1, 1, 1, 1, 1], target of 110 (even the whole road sums to only 5)
reward
320 XP
signature
shortestBinding
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…
  • classic road
  • single totem suffices
  • never enough power
  • empty road
  • whole road exactly
  • pair in the middle
  • lone strong totem
  • three at the tail
  • all equal totems
  • single totem road short
  • exact single among many
The Totem Road of the Quillfang Steppe · bitwick