Deep-Space Scan Windows

Best totals for each exact number of bounded-length intervals

HardDynamic ProgrammingMonotonic QueuePrefix Sums

A deep-space probe records one telemetry digit per second. Its record is the string readings. A digit contributes its numeric value minus 5 to the scientific value of a scan: for example, '9' contributes 4, while '0' contributes -5.

A scan window is a nonempty contiguous interval of the record whose length is between min_length and max_length, inclusive. Its value is the sum of the contributions in that interval.

Selected windows must occur in chronological order and must not overlap. Between consecutive windows, at least cooldown seconds must remain unselected. There is no required cooldown before the first window or after the last window. When cooldown is zero, consecutive windows may touch and still count as separate windows.

Return a list of length max_windows. Its element at index c - 1 must be the largest possible total value when selecting exactly c windows, for every c from 1 through max_windows.

Each list element describes an independent optimization; the selected windows need not be shared between different elements. Negative totals must be retained rather than replaced by zero. The input guarantees that selecting max_windows windows is possible.

Examples

Example 1

Input: readings = "9183097", min_length = 1, max_length = 2, cooldown = 1, max_windows = 2
Output: [6,10]

For one window, the final two digits form an optimal choice. For two windows, a single-digit window at the beginning can be combined with that final window, leaving the required cooldown.

Example 2

Input: readings = "22222222", min_length = 2, max_length = 3, cooldown = 1, max_windows = 3
Output: [-6,-12,-18]

Every selected second has negative value, so each window should have the minimum permitted length. Selecting three windows uses all eight seconds once the required gaps are included.

Example 3

Input: readings = "975791", min_length = 2, max_length = 4, cooldown = 0, max_windows = 2
Output: [8,12]

There is no required gap, so two selected windows may touch. Nevertheless, each window must independently satisfy the length bounds.

Constraints

  • 1 <= readings.length <= 100000
  • readings contains only the characters '0' through '9'
  • 1 <= min_length <= max_length <= readings.length
  • 0 <= cooldown <= readings.length
  • 1 <= max_windows <= 20
  • max_windows * min_length + (max_windows - 1) * cooldown <= readings.length

An O(max_windows * readings.length) time solution with O(readings.length) auxiliary space is expected. Window endpoints can be viewed as half-open intervals [start, end); the cooldown rule is next_start - previous_end >= cooldown.

Hints

Show hint 1

Let a state describe the best total from a prefix using an exact number of windows. If the last window starts at j and ends just before i, which prefix may contain the previous windows?

Show hint 2

For one fixed window count, separate the last window's prefix-sum contribution into a term depending only on i and a term depending only on j. The eligible starts form a moving interval.

Follow-up questions

What an interviewer might ask once you have a working solution.

  • How would you reconstruct one optimal collection of windows for a requested exact count?
  • How would the transition change if the c-th window's value were multiplied by a given coefficient that could be negative?

Practice this with an AI interviewer

Explain your approach out loud, write Python or JavaScript, run it against hidden tests (including large inputs), and get a scored debrief.

Start this problem