Basis Lab

The Patterns Behind Most Coding Problems

Totals You Compute Once, Then Read by Subtraction

Last timeRemembering What You Have Seen

Build the running totals once and every question about the total of a stretch becomes one subtraction, including counts, rectangles and the stretch a window cannot find.

Somebody asks for the total of one stretch of a list, then for the total of

another, then another. Each answer on its own is a loop over the stretch. The

structure being used here is neither sorted order nor adjacency. It is that the

totals of the stretches are not independent of one another, because two

stretches that begin at the same place differ by exactly the values lying

between their right ends. One table, built once, turns every one of those

questions into a subtraction.

The working list throughout is A=[2,7,11,15,1,8,4,4,9,3]A = [2, 7, 11, 15, 1, 8, 4, 4, 9, 3], ten

values totalling 64.

The table, and the entry that is not a value

Write PkP_k for the total of the first kk values of AA. The first kk means

the values at positions 0 to k−1k - 1, which is the half-open reading, and that

one decision is what the rest of this rests on. P0P_0 is the total of no values,

which is 0, so eleven entries describe ten values.

FIG 1Building the table of running totals for A
stepkthe value at position k minus 1P at k, the total of the first k valueswhat happened
10nothing yet0the total of no values, and the entry that makes stretches starting at position 0 ordinary
21220 plus 2
32792 plus 7
4311209 plus 11
54153520 plus 15
6513635 plus 1
7684436 plus 8
8744844 plus 4
9845248 plus 4
10996152 plus 9
111036461 plus 3, and the last entry is the total of the whole list
11 steps
Every entry is the entry above it plus one value, so the table costs ten additions for ten values rather than one pass per entry. That is the same trick the table is about, used on the table itself, and getting it wrong is the quiet way to turn a linear precomputation into a quadratic one while still calling the lookups constant.

So Pk=Pk−1+Ak−1P_k = P_{k-1} + A_{k-1}, ten additions. Now the identity that makes the

table worth having. The values from position ii to position jj inclusive are

the first j+1j + 1 values with the first ii values taken away.

The lesson stops here

9 more paragraphs to go

You have read the opening. The rest of the argument, the problems that check whether it landed, and the lines worth keeping at the end all come with a plan.

The first lesson of every course in the library reads the whole way through, free, so you can see exactly what the rest of them are.

See the planThe contents

This is the reading half

Starting the course gives you your own copy of it. Every idea on every page has problems standing under it, marked with a reason rather than a tick, and any sentence you do not believe can be opened and argued with. None of that can happen on a page nobody owns.

The contents

The rest of this course

  1. 01What a Pattern Is Actually Exploiting, and Why It Is Never Cleverness
  2. 02Two Ends Moving Inward, and the Proof That Nothing Is Lostopening only
  3. 03A Window That Slides, and Why Its Work Stays Linearopening only
  4. 04Remembering What You Have Seen, and What the Memory Costsopening only
  5. 05Totals You Compute Once, Then Read by Subtractionyou are here
  6. 06Keeping Only the Best Few Without Sorting the Restopening only
  7. 07Searching the Answer Instead of the Inputopening only
  8. 08Choosing the Pattern From the Words of the Problemopening only

Read alongside