I have some data like this:
1: 2 - 10
2: 3 - 15
3: 4 - 9
4: 8 - 14
5: 7 - 13
6: 5 - 10
7: 11 - 15
I will attempt a representation to make it clearer:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
1 |--------------------------------------X---------|
2 |--------------------------------X--------------------------------------------|
3 |--------------------------X---|
4 |-X-------------------------------------|
5 |--------X------------------------------|
6 |--------------------X----------|
7 |---------------------------|
So in the example case, 8-9
is the critical period if the second scheme is used because all the points are active. What is a fast and good way to solving this problem in python? I am thinking of using dynamic programming but are there other approaches that are suggested?
My approach until now:
I was thinking more from a real-time perspective. So, whenever I get a new point, I do this: Assume I already got 2-10
and I get 3-15
then I pick the max of start and min of end so this case it is 3-10
and increment this interval's count to 2. Then the third point comes in 4-9
, pick the max which is 4 and the min is 9 and update the value 3-10
to 4-9
and update count to 3. Now when 8-14
comes in, I pick the start of this interval is greater than 4-9
and the end of this interval is less than 4-9
. In this case, it is not true so I will create a new bucket 8-14
and I put the count to 1. This is not the entire algorithm but should give a high-level idea of what I am doing here. I will see if I can sketch the pseudo-code.