Hi , obviously this problem can be solved in O(N*M) with help of an array for counting, because numbers are <=1000 . but what is the best algorithm for solving this problem when numbers are <=1e9 ?
I wrote this code that works in O( N*M*log(N*M) ) but it got TimeLimit ( in 1second ) . How can we reduce this complexity ?
UPD : sorry , I linked to wrong source code. fixed.
The first thing I came up with was a combination of unordered_map and set, which should have fit into the time limit, but somehow it didn't. So I tried a few things, some got MLE and others TLE again.
What I ended up with is a code combining lists of people for each chat count, along with a variable storing the minimum chat count that gets updated dynamically and an unordered_map for keeping count of chats for each person. It's rather messy, but it worked (393 ms).
Here's the code: C++11 Code
Thanks , that's very interesting! how we can compute it's complexity ?
Auto comment: topic has been updated by bluemmb (previous revision, new revision, compare).