bluemmb's blog

By bluemmb, history, 9 years ago, In English

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.

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

»
9 years ago, # |
Rev. 2   Vote: I like it +3 Vote: I do not like it

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

  • »
    »
    9 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Thanks , that's very interesting! how we can compute it's complexity ?

»
9 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by bluemmb (previous revision, new revision, compare).