Try every split
transcript
The last merge splits a range in two. Where should it fall? Try every place.
The whole row has four splits. Each adds two parts and 23. The totals are 59, 55, 53, 57. The least is 53.
The state is the cost of a range. The transition tries every k. A single shard costs zero.
There are about n squared over two ranges. Each tries up to n splits. Two hundred items need 1,333,300 checks.
The loop runs k from i to j minus one. Stopping one short skips a split. For 1, 1, 2 it returns 7, not 6. Test the loop bounds on one item and on equal items. Bounds are where interval bugs hide.
Merging, triangulation and splitting a log file break a range in two. Retiring ad slots chooses the last item removed. The row game peels one end. All three fill ranges by length.
Try every split, and keep the least. Cubic time, square memory. Full breakdown in the lecture.