Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Simple defaultdict solution | O(n) time complexity solution in Clear category for When "k" is Enough! by swagg010164
from typing import Iterable
from collections import defaultdict
def remove_after_kth(items: list, k: int) -> Iterable:
st = defaultdict(int)
for it in items:
st[it] = min(st[it] + 1, k)
ans = []
for it in items:
if st[it] > 0:
ans.append(it)
st[it] -= 1
return ans
Oct. 22, 2022
Comments: