I got only 30 pts, could not figure out the reason.
I was using approach for 100 pts using (multisets of pair). I inserted each element with row number in “rows” multiset and with column number in “columns” multiset. Then iterated over all elements finding the row with the max frequency of each element, then storing these row numbers with max frequency in another vector.The same procedure for columns.
how is it that the iterating over all max row and max column combinations of each number amounts to only NXM ? Isn’t it possible that a single row can be the max row for many numbers and similarly for columns? for example this 4X4 matrix:
{1 1 2 2},
{2 2 1 1},
{1 1 2 2},
{2 2 1 1}
int num = a[pi][pj];
if (pos[num].size() > 0){
int bestx = 0, besty = 0;
BX.clear(); BY.clear();
for (int p = 0; p < pos[num].size(); p++){
int x = pos[num][p].first;
int y = pos[num][p].second;
Yes, as long as we use arrays. Using hash tables or binary search trees could add overheads that induce TLE. Sample implementation - CodeChef: Practical coding for everyone
Thanks i got it, i was trying a lot of that kind of approach and endded up in TLE , and the overall complexity would be more than O(NxM) ,doesnt it depend on the number of distinct characters
i think inserting in multiset would be taking larger time. Think for the case when all numbers are distinct and N*M is 10^6. This happened with me when i was using set.
Note I mentioned O(NM), so within a constant factor of N*M).
The key is that the more times a row/column is best for a specific number, the less distinct numbers we have. While in your example both 1 and 2 have 4 options for each row/column, there are only 2 numbers to consider.
Your link says “Access Denied.” This link is the real link to your solution. Also, I might be wrong, but I do not think you are accounting for the case where you need to subtract 1 because the row and column have the number in question at its intersection.