Python Max Counters
def max_counters(n: int, a: list[int]) -> list[int]:
counters = [0] * n
max_counter = 0
last_update = 0
condition = n + 1
for v in a:
if v <= n:
index = v - 1
if counters[index] < last_update:
counters[index] = last_update
counters[index] += 1
max_counter = max(counters[index], max_counter)
if v == condition:
last_update = max_counter
for k, v in enumerate(counters):
if v < last_update:
counters[k] = last_update
return counters
This delays the expensive “set all counters to max” work until it is really needed, which keeps the solution fast.