CipheredBytes's blog

By CipheredBytes, history, 12 months ago, In English

In my recent attempt with 1701C - Schedule Management, I encountered a situation where I need to count the frequency of tasks assigned to machines and then process this information in a certain way. I implemented two different approaches, one using a manual approach with arr[x-1] and the other using Counter to get task frequencies.

Here is a brief explanation of both approaches:

First Approach (Works Fine): 315769984

arr = [0] * n
for x in li():
    arr[x - 1] += 1
arr.sort(reverse=True)

Second Approach (Causes Time Limit Exceeded): 315770224

from collections import Counter

arr = li()
 
arr = list(sorted(Counter(arr).values(), reverse=True))
arr.extend([0] * (n - len(arr)))

Issue: This approach causes a Time Limit Exceeded (TLE) error, even though it seems like a more optimized approach.

Could it be due to hash table overhead ?

Full text and comments »

  • Vote: I like it
  • +1
  • Vote: I do not like it