Answer the question
In order to leave comments, you need to log in
Algorithm for finding the number of intersections of segments in a sequence (list)?
You can write an algorithm that is minimal in
complexity
(time) to find the number of intersections
of
segments
in
a
sequence
(
list
)
Answer the question
In order to leave comments, you need to log in
Get an array of zeros. For each segment, make +1 to the coordinate of its left end and -1 to the coordinate following the right end.
Then walk through the array from left to right, maintaining the counter. Add the value of the array to the counter and output the value of the counter.
This is if you need to output the entire array, as in the example. I don’t know the specifics of your task, maybe it will be more efficient to put pairs {coordinate, + -1} into the array and sort. Then, in the same way, go around from left to right while supporting the counter.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question