One address per cell
transcript
A grid holds scores for ads. A report wants them along diagonals. Searching for each cell would be slow. Compute the address of every cell instead.
A cell has a row and a column. Its diagonal is the sum of the two. All cells with the same sum share a diagonal.
The first diagonal is the single cell one. The second is read from the top: two, five. The third is read from the bottom: nine, six, three.
A fixed direction gives the wrong order. Use the parity of the diagonal number. Even goes up and odd goes down.
Count the rows plus the columns. Subtract one for the shared corner. Three by four gives six diagonals.
Computing an address costs a single sum. No search is needed. So each cell costs constant time to place. The whole grid is visited exactly once.
Write the address as a formula. Visit cells by that address. Alternate the direction by parity. Full breakdown in the lecture.