Verify every time you create a new one. First find out the set that intersects with the currently created one, and then verify whether there are any two-by-two intersections in the set. If there is, then it will not be created, thus ensuring that they will not exist at the same time. Three announcements
When inserting, traverse the table, determine whether the current timestamp is between two timestamps, and record the number that meets this condition. If >= 3, the insertion will not be performed. Otherwise, insert into the data table.
Classic line segment coverage problem. I don’t know Java, so I’ll give you an O(n) idea.
First find all the intervals that intersect with the interval to be verified, Sort by the left endpoint of the interval from small to large
Remember the first interval as CurrentInternal
For CurrentInternal, examine its next item NextInterval: if it does not intersect with CurrentInternal, record it as CurrentInternal and jump to 2; otherwise, record its intersection interval as Intersection.
For Intersection, traverse the items after NextInterval: If there is no interval that intersects with Intersection, then record NextInterval as CurrentInterval and jump to 2; otherwise, it means there are three intersecting intervals and exit.
If the entire list is traversed, it proves that the interval to be verified is legal.
A DEMO written in JS is attached, I hope you all have fun
Verify every time you create a new one. First find out the set that intersects with the currently created one, and then verify whether there are any two-by-two intersections in the set. If there is, then it will not be created, thus ensuring that they will not exist at the same time. Three announcements
Just write sql and it’s done:
If the result is less than 3, create it.
When inserting, traverse the table, determine whether the current timestamp is between two timestamps, and record the number that meets this condition. If >= 3, the insertion will not be performed. Otherwise, insert into the data table.
Use two time points to count how many effective announcements there are in this time period!
You cannot create more than 3 items.
A stupid way to find out all the times that overlap with new announcements, and then make statistics by day
I tried to write it down, you can refer to it,
Classic line segment coverage problem. I don’t know Java, so I’ll give you an O(n) idea.
First find all the intervals that intersect with the interval to be verified, Sort by the left endpoint of the interval from small to large
Remember the first interval as CurrentInternal
For CurrentInternal, examine its next item NextInterval: if it does not intersect with CurrentInternal, record it as CurrentInternal and jump to 2; otherwise, record its intersection interval as Intersection.
For Intersection, traverse the items after NextInterval: If there is no interval that intersects with Intersection, then record NextInterval as CurrentInterval and jump to 2; otherwise, it means there are three intersecting intervals and exit.
If the entire list is traversed, it proves that the interval to be verified is legal.
A DEMO written in JS is attached, I hope you all have fun
https://jsfiddle.net/hsfzxjy/7td0rwr2/28/