• + 0 comments

    I've seen this approach in part 1 of "algorithms" course on coursera. It was used to find intersections between horizontal and vertical line segments on a plane. You're right, with large number of queries the sorting part might be a problem. But it can be improved a bit. When you create operations, do not put them on the list, but in a hash map where the keys are the indices from operation. When next operation is added, and there are already operatons with the same index, you only add the new operation's value to the value of existing operation. Then the number of operations to sort will be limited by n. But still, if the number of operations to sort is sufficiently close to n, the approach with array of length n should be faster.