Why it matters

Fractional knapsack shows when greedy wins over 0/1. Understanding shapes technique.

Advertisement

The architecture

Sort by v/w desc.

Take fully while fits.

Take fraction of next.

Fractional knapsack flowSort by v/wdescendingTake fullywhile fitsTake fractionfill remainder0/1 knapsack (no fractions) requires DP; fractional greedy works only with divisibility
Fractional knapsack.
Advertisement

How it works end to end

Exchange argument.

O(n log n) sort dominates.

0/1: NP-hard-ish (pseudo-poly DP).