This is the following problem. CSES Problem Set Movie Festival II This is my approach (https://cses.fi/paste/4d6196360ee8fd8e21094e/) It's failing a few cases I wanted to know if my approach is completely wrong or I'm not handling some cases properly.
I am not sure about the proof, but sort intervals by their end time , then greedily assign the Interval to the member who is currently free,and change his status from free to busy, now when the interval of that person ends you can count that person as free to watch again.
The mistake that you are making is that you just consider the earliest person that started watching a film. What could happen is that might assign a new movie to a person which has been free from some time but that could come in handy later on.
Example:
4 2
5 6
4 9
10 12
7 14
Now, according to your algorithm, the following would happen-
You will assign one person to watch the first movie (5, 6).
You will assign the next person to watch the second movie (4, 9).
Now, you will see that the first person is free, so you will assign the first person to watch the third movie (10, 12).
Now, the fourth movie cannot be watched by anyone. So the answer according to you is 3.
The actual answer however is 4 as if you assign the second person to watch the third movie, then the first person can also watch the 4th movie.
You should keep a track of the active movie watchers in a multiset instead of a queue and take the nearest option instead of taking the first person in the queue. I am attaching my code for your reference.