Here's a task:
Curators of the summer flowers exhibition were preparing for the event. They had to find a time during which the biggest amount of flowers were blooming. They've had a table which contained (0<n<30) flowers and their properties:
ID, the start and the end of blooming season (StartMonth, StartDay, EndMonth, EndDay)
Help the curators find the earliest time during which most flowers bloom(the start and the end of the interval) for the exhibition.
Note: the right time does not necessarily have to be an already existing interval
Since this is an assignment I'd like to do the most of it by myself, I'd just like to get an advice on how to theoretically solve this problem.
A sample of the blooming seasons:
121 6 15 7 25
102 7 1 8 14
236 6 30 8 31
141 7 31 8 10
111 7 1 7 20
128 6 2 6 3
To start with I'll introduce my approach:
Now thinking about it an idea popped in my mind is that i could try to just make a struct of 92 days and then count which days contain most blooming seasons. This would be pretty easy but I wouldn't be happy with this solution since it would be too inefficient. (Chuckles) What about when compared to my current code which matches intervals again and again. Would it still be inefficient? :D
I've also seached the internet for the answer but managed to find only the half of it.
All in all I'd love to hear other opinions. What are the ways to properly deal with this? Is my approach reasonable or should I look for another one? Your help would be greatly appreciated. Again I'm not asking for a full soliution, just nudge towards the answer. Thanks in advance.
Sort all interval bounds by increasing date, weighting them +1 for a beginning and -1 for an ending.
Now scan the list in chronological order, adding the point weights as you go. Every time, this gives the number of different blooming flowers. In your case, 012345456... 0.
At the same time, keep the bound giving the highest count so far (in case of ties, keep the earliest value). The end of the highest interval is simply the next bound.
If several intervals begin or end simultaneously, process the ones ending first, to avoid zero-duration solutions. (To achieve this, it suffices to craft a comparison function such that in case of tie, -1 has precedence.)