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 ?

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

»
12 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

https://mirror.codeforces.com/blog/entry/62393

Essentially, try to avoid using Counter, map, or set in python because people can hack your solutions with special test cases that cause each get operation to take O(N) time