In this submission -> 306874451 as you go through the solve function you will see that I have used
vector<ll> freq(2e5+1,0);
as you can see it got Time Limit exceeded on test case 2, after replacing the vector with map in the following submission -> 306878312
map<ll,ll> freq;
It got accepted, now according to my knowledge map takes O(log N) time to access its elements while vector takes O(1) time.
So in the worst case scenario shouldn't the vector be better ?
Problem Link -> 1775B - Gardener and the Array
[BTW This is my first blog so please forgive me if I made any mistake.]
Auto comment: topic has been updated by alphaaditya (previous revision, new revision, compare).
In each test case, you are creating a vector of size 2e5. So in total 1e5 test cases* 2e5 size of vector created = 2e10, which will obviously give tle.
Map is dynamic so will never reach the size, even if at worst case, k can be at most 1e5 and in that case t would be 1 as it is guaranteed that sum of k won't cross 1e5.
but then shouldn't it give memory limit exceeded error instead of time limit exceeded ?
Not necessarily — your solution used both too much memory and time, but probably exceeded the TL before the ML
Oops, sorry, the memory usage is fine. beaaaan is correct
The arrays created in the test cases doesn't co-exist, once it's done being used, it'll free itself, so no, it'll not MLE.
However, creating and allocating an array takes $$$\mathcal{O}(n)$$$
Ok so if creating and allocating an array takes O(n) time then for each test case I am creating and allocating 2e5+1 size array so for all the test cases the time complexity could be 1e5 * 2e5 = 2e10 which results in TLE. is this correct ?
Yes, this is correct
Thanks for the help.
Memory of vector is freed when it goes out of scope so mle isn't exceeded. It's just allocation time that's taking a lot of time giving tle.
Got it Thanks for the help.
you are creating a vector of size 2e5+1 this takes a lot of memory, on the other side, each key in the map is inserted dynamically
yes you are right but if it is consuming too much memory shouldn't the error be memory limit exceeded ?
Bro the declaration of the vector had made a complexity of $$$O(TN)$$$。 if you want to use the vector, you have to declare it globaly or just apply for a vector with size $$$N$$$ every time.