Learning pathsPricingChecking sign-in…
Trees, graphs and optimization

Greedy reasoning

Prove a local decision preserves an optimal solution.

A locally attractive move needs an exchange, cut, stays-ahead, or dominance argument.

Mechanism and state

Jump Game permits every distance up to nums[i], so reachable positions form a contiguous prefix. Store only its farthest boundary. Extend that boundary from reachable positions; a gap cannot be rescued by an unreachable later index. This compressed feasibility state does not count minimum jumps or remember an actual path. Dense Prim keeps one single-edge connection key per outside point. Admit the unused vertex with the minimum key, add that key once to the tree total, then update outside keys using Manhattan distance to the new point. The root enters with a virtual zero key. No complete pair list is needed. Minimum jumps upgrades a feasibility frontier into complete BFS layers: scan all current sources before charging another jump. Gas Station separates global fuel conservation from a resettable candidate tank. Partition Labels grows a character closure to its farthest last occurrence and cuts at the earliest safe point. Queue reconstruction sorts height descending and equal-height k ascending, inserts at k, then checks arbitrary-input feasibility. One-transaction stock profit keeps the cheapest strictly earlier buy independently of completed profit.

Why the pattern works

Assume every index through farthest is reachable. A reachable i contributes another interval [i,i+nums[i]], extending the same prefix without holes. If i exceeds farthest, no later starting point is reachable. Thus frontier extension preserves exactly the information needed for feasibility. The lightest edge crossing the selected/unselected cut is safe: insert it into an optimal tree, remove another edge crossing that cut from the resulting cycle, and obtain an equally good tree containing the greedy choice. This exchange proof differs from shortest-path finality even though both algorithms select a minimum label. BFS layers contain every endpoint available within their jump budget, so the first destination-covering layer is minimum. A failed fuel segment has nonnegative earlier prefixes; subtracting those from its negative sum eliminates every interior start. A minimum global prefix supplies a feasible circular start when total fuel is nonnegative. Character obligations forbid every cut before closure and allow refinement at closure. Later shorter queue insertions do not affect taller qualifying counts; equal heights require increasing k. A cheaper earlier buy weakly improves every future sale, giving a dominance argument.

Work through an example

[3,2,1,0,4] reaches only through index 3 and fails at 4. [2,0,0] succeeds despite zeros. In contrast, largest-first coin choice fails for [1,3,4] and amount 6 because local denomination size does not preserve optimal count. In DSA-117 a newly selected point can lower several outside connection keys without adding its own admission cost to those candidates. Duplicate coordinate positions are distinct vertices joined by zero-cost edges. Ascending scans choose the smallest unused index on ties. In [2,3,0,0,0], the first jump frontier ends at two but its interior index one reaches the destination in two jumps. gas=[0,0,5], cost=[1,1,1] rejects starts zero and one, then succeeds from two. In abac, a forces a length-three first part and c remains independent. Inserting [5,0] ahead of [6,0] changes the taller person’s position but not its qualifying count. Stock prices [2,4,1] yield profit two, because the global minimum comes after the best sale.

Where the reasoning fails

Exact-jump-only movement can create holes, invalidating this prefix summary. Always state the choice model. Use long before adding an index and a large jump. Do not label a heuristic greedy proof merely because examples pass. Prim keys are attachment-edge costs, Dijkstra labels are root-route costs, and minimax labels are largest-jump costs. Mixing their update formulas changes the optimization problem. A dense complete graph makes O(n²) scanning reasonable; a heap alone does not remove the need to inspect implicit candidate pairs. A stalled jump layer returns −1; do not assume reachability. Never reset Gas Station’s global total or reject a zero tank. Partition output must maximize cuts, not merely be valid. Queue duplicates [5,0],[5,0] are infeasible even when insertion indices fit. Stock profits cannot combine separate positive swings under a one-transaction contract. Each lesson validates its complete input before early outcomes and states its copying and numeric policy.

Reconstruct the reasoning

For a proposed greedy rule, identify the invariant and construct a plausible counterexample first. Explain which changed constraint would make your proof stop applying. State the cut property, trace the five-point Manhattan example to total twenty, and contrast its selected connections with a root star. Compare an all-pairs Kruskal implementation using DSU with dense Prim’s O(n) workspace. Trace one failure and one success for each of the five new greedy lessons. Explain why exact-length jumps, finite fuel capacity, streaming final-occurrence information, reversed equal-height ordering, and unlimited stock transactions each require a changed model or proof. Distinguish constant reference workspace from teaching histories and quadratic queue insertion costs.

Apply this foundation

Your study notes