Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | 1x 1x 50000x 50000x 50000x 50000x 8560000x 25730000x 25730000x 51560000x 8560000x 8560000x 25730000x 25730000x 25730000x 16020000x 16020000x 9710000x 25730000x 25730000x 51560000x 8560000x 50000x | // Increase for faster but less accurate results. const PIXEL = 1; // Returns a layout where each gap is within PIXEL of optimal. export function find_optimal(m: Array<Array<number>>) { const n = m.length; let positions = Array(n).fill(0); let gaps = Array(n).fill(0); // eslint-disable-next-line no-constant-condition while (true) { // Compute the leftmost layout. for (let j = 1; j < n; j++) { positions[j] = positions[j - 1] + gaps[j - 1]; for (let i = 0; i < j; i++) { positions[j] = Math.max(positions[j], positions[i] + m[i][j]); } } // Compute the rightmost layout. As we do so, each gap that can grow, grows. let stillGrowing = false; for (let i = n - 2; i > -1; i--) { let proposedGap = gaps[i] + PIXEL; // i + 1 is rightmost; i is leftmost. let maxGap = positions[i + 1] - positions[i]; if (proposedGap < maxGap) { gaps[i] = proposedGap; stillGrowing = true; } else { gaps[i] = maxGap; } positions[i] = positions[i + 1] - gaps[i]; for (let j = n - 1; j > i; j--) { positions[i] = Math.min(positions[i], positions[j] - m[i][j]); } } if (!stillGrowing) { return positions; } } } |