mirror of
https://github.com/kuhyx/utils.git
synced 2026-11-22 14:12:31 +01:00
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WzxdWXLvi9aq4MxL4XHKJ4
954 B
954 B
Skeleton: Heap and dynamic programming (heap-dp)
Signal: O(log n) push and pop-min (a heap, partial order); an answer built from answers to smaller inputs (a DP table or a memo). Trap: name what ONE cell of the table means before you fill it, and size the table for the base case; JavaScript has no built-in heap, so keep one in an array.
// Bottom-up table.
const cell: number[] = new Array(/* HOLE: how many states, base state included */).fill(0);
cell[0] = /* HOLE: the base state */;
for (let i = 1; i < cell.length; i++) {
cell[i] = /* HOLE: from cell[i - 1], cell[i - 2], ... */;
}
const answer: number = cell[/* HOLE: which state is the answer */];
// Heap kept in a plain list: the parent of slot i is (i - 1) >> 1.
function siftUp(h: number[], i: number): void {
while (i > 0) {
const up: number = (i - 1) >> 1;
if (/* HOLE: parent already in order */) { break; }
/* HOLE: swap h[i] and h[up] */
i = up;
}
}