TypeScript Max Counters
function maxCounters(n: number, a: number[]): number[] {
  const counters: number[] = new Array(n).fill(0);
  let maxCounter = 0;
  let lastUpdate = 0;
  const condition = n + 1;

  for (const v of a) {
    if (v <= n) {
      const index = v - 1;
      if (counters[index] < lastUpdate) {
        counters[index] = lastUpdate;
      }
      counters[index]++;
      maxCounter = counters[index] > maxCounter ? counters[index] : maxCounter;
    }
    if (v === condition) {
      lastUpdate = maxCounter;
    }
  }

  return counters.map((v) => (v < lastUpdate ? lastUpdate : v));
}

This delays the expensive “set all counters to max” work until it is really needed, which keeps the solution fast.