‹ back to the board

Wednesday, October 21

Adeptalgorithms & data structures

The Casket-Barge of the Blackmere

Posted by the Thornwake Bargemen's Guild.

The barrow-caskets of the old marsh-kings of Ostrell stand in a single line on the eastern quay of the Blackmere. The ward laid on them forbids moving them out of that order. One spell-barge will ferry them to the western shore. Each crossing takes the next unbroken run of caskets from the front of the line. The barge may make at most trips crossings before the ward fades. The Guild must enchant the barge's hull with a weight limit, and every point of limit costs silver, so they want the smallest limit that gets every casket across.

Write minBargeLimit(weights, trips).

  • weights is a list of integers, each at least 1. These are the casket weights in their fixed order. The list may be empty. It has at most 10,000 entries, and each weight is at most 1,000,000.
  • trips is an integer, at least 1.

Return the smallest integer C that satisfies the following. The list weights can be split into at most trips contiguous groups, kept in their original order, so that the sum of every group is at most C. Every casket belongs to exactly one group, and no casket can be split. You do not have to use every crossing.

If weights is empty, return 0.

  • binary search
  • Adept
  • efficiency bonus

examples

  1. case 1weights of [7, 2, 5, 10, 8], trips of 218. The crossings carry [7, 2, 5] = 14 and [10, 8] = 18, and no smaller limit manages it in two crossings.
  2. case 2weights of [4, 9, 2], trips of 109. Spare crossings go unused, but the barge must still carry the 9-weight casket alone.
  3. case 3weights of [], trips of 30
reward
360 XP
signature
minBargeLimit
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 two crossings
  • ten caskets five crossings
  • three crossings
  • more crossings than caskets
  • single crossing takes all
  • empty quay
  • single casket
  • heaviest casket decides
  • hundred equal caskets
  • alternating heavy and light
The Casket-Barge of the Blackmere · bitwick