# Pick the k items that cover the most

> Lazy greedy keeps every candidate in a max heap keyed by its last known gain and re-evaluates only the top.

**Few items**

```ts
import { mapInputOrder, maxBinaryHeap, numberOrder } from 'knotkit/heap'

// Each candidate reaches some users; pick the two that reach the most users together.
const audiences = [
  ['ada', 'grace', 'alan'],
  ['grace', 'alan'],
  ['edsger', 'barbara'],
  ['ada', 'edsger'],
]

const reached = new Set<string>()
const gainOf = (index: number): number => (audiences[index] ?? []).filter((user) => !reached.has(user)).length

interface Candidate {
  readonly index: number
  /** The gain when last computed: an upper bound, because gains only shrink as the set grows. */
  readonly gain: number
}

const byGain = mapInputOrder(numberOrder, (candidate: Candidate) => candidate.gain)
const frontier = maxBinaryHeap(
  byGain,
  audiences.map((_, index) => ({ index, gain: gainOf(index) })),
)

const picks: number[] = []

while (picks.length < 2) {
  const top = frontier.peek()
  const fresh = gainOf(top.index)

  if (fresh < top.gain) {
    frontier.replaceRoot({ ...top, gain: fresh }) // stale bound: lower it and look again
    continue
  }

  frontier.pop()
  picks.push(top.index)

  for (const user of audiences[top.index] ?? []) {
    reached.add(user)
  }
}

picks // [0, 2]
reached.size // 5
```

**Thousands of items**

```ts
import { mapInputOrder, maxBinaryHeap, numberOrder } from 'knotkit/heap'
import { splitmix32, uniformInt } from 'knotkit/prng'

const random = splitmix32(2026)
const k = 20

// 5,000 candidates over 2,000 users. Each candidate reaches between 1 and 60 seeded users, so audiences overlap.
const audiences = Array.from({ length: 5_000 }, () => {
  const size = 1 + uniformInt(random, 60)

  return new Set(Array.from({ length: size }, () => uniformInt(random, 2_000)))
})

const reached = new Set<number>()
let evaluations = 0
const gainOf = (index: number): number => {
  evaluations += 1
  let gain = 0

  for (const user of audiences[index] ?? []) {
    if (!reached.has(user)) {
      gain += 1
    }
  }

  return gain
}

interface Candidate {
  readonly index: number
  /** The gain when last computed: an upper bound, because gains only shrink as the set grows. */
  readonly gain: number
}

const byGain = mapInputOrder(numberOrder, (candidate: Candidate) => candidate.gain)
const frontier = maxBinaryHeap(
  byGain,
  audiences.map((_, index) => ({ index, gain: gainOf(index) })),
)

const picks: number[] = []

while (picks.length < k) {
  const top = frontier.peek()
  const fresh = gainOf(top.index)

  if (fresh < top.gain) {
    frontier.replaceRoot({ ...top, gain: fresh }) // stale bound: lower it and look again
    continue
  }

  frontier.pop()
  picks.push(top.index)

  for (const user of audiences[top.index] ?? []) {
    reached.add(user)
  }
}

picks.length // 20
reached.size // 999
evaluations - audiences.length // 2853, after seeding the heap
k * audiences.length // 100000, re-scoring every candidate for every pick
```

Gains only shrink as the selection grows, so a stale gain is an upper bound: when the refreshed top still beats every other bound, it is the greedy choice without evaluating the rest. [The algorithm page](/problems/budget-allocation/) explains the guarantee and the measured savings.

**Modules:** [`knotkit/heap`](/modules/heap/) · [API reference](/reference/knotkit/heap/)
