I am trying to find out the logic to solve this problem , KOIREP — Representatives , i tried for 7 days still get no idea to solve it .
Can anyone please tell me how can i effeciently solve it ? Problem tagged with Sliding window alias two pointer .
# | User | Rating |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3831 |
3 | Radewoosh | 3646 |
4 | jqdai0815 | 3620 |
4 | Benq | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | gamegame | 3386 |
10 | ksun48 | 3373 |
# | User | Contrib. |
---|---|---|
1 | cry | 164 |
1 | maomao90 | 164 |
3 | Um_nik | 163 |
4 | atcoder_official | 160 |
5 | -is-this-fft- | 158 |
6 | awoo | 157 |
7 | adamant | 156 |
8 | TheScrasse | 154 |
8 | nor | 154 |
10 | Dominater069 | 153 |
I am trying to find out the logic to solve this problem , KOIREP — Representatives , i tried for 7 days still get no idea to solve it .
Can anyone please tell me how can i effeciently solve it ? Problem tagged with Sliding window alias two pointer .
Name |
---|
This could be solved using two pointers. Firstly sort all numbers and for each of them remember the group ID to which it belong to. Then for each position l in sorted array we can find the smallest possible r such as there are n different groups on the segment from l to r. Time Complexity : O(n * m * log(n * m));
awesome thanks
You're welcome! :)
how can I efficiently implement "distinct group from index L to R "?. I was thinking that inserting group no. into set, set size will tell me number of distinct group from L to R, I will erase a data from set if the frequency of that data from index L to R becomes 0. But inserting into set complexity is nlog2n.
You can just keep a cnt[] array where cnt[i] tells you the number of elements of the i'th Group ID. Now keep a variable answer. Whenever you move a pointer update cnt[i] i.e. if it comes inside the sliding window then increase otherwise decrease it. If cnt[i] becomes one then answer++ and if cnt[i] becomes 0 then update answer--. This is a common technique and it it will be beneficial to get familiar with it.
Can you please share your solution?
My approach is similar to what mentioned above. You can see my code here.
I did it storing pointers to every class and moved only that pointer which has least ability(made sure I sorted every class) if you want I can share my solution.