There is an asymptotically faster way to solve this problem, but I have serious doubts as to whether it will work faster in practice, because the size of your window (10) is so small. If you want to increase this size, which I will call k, to be larger, you may want to choose the approach shown below.
When you use this algorithm, you need to maintain a window of k elements that support two operations:
- Insert a new item, choosing the oldest.
- Returns all items that exceed some values.
One way to do this is to store all of your elements in a data structure that combines a balanced binary search tree and a queue. The queue contains all k elements stored in the order in which they are displayed in the original sequence, and is used so that we can remember which element to evict when we need to add a new element. Balanced BST stores a copy of each of these items stored in sorted order. This means that you can implement the operations described above as follows:
- Insert a new item by cutting out the oldest: Add a new item to the queue and BST. Then exit the queue to get the oldest item, then remove it from the BST. Runtime: O (log k), since BST has k elements in it.
- Returns all elements that exceed some value:. Using BST, find the smallest element at least as large as the O (log n) value. Then scan through BST and list all elements at least as large as that element. This takes O (z) time, where z is the total number of matches found.
In total, if you have n elements and only z pairs that you need to insert into the database, this algorithm will take O (n log k + z) time. To verify this, note that we perform a total of n copies of operation (1), each of which takes O (log k) time. We also perform n copies of operation (2), which takes O (n log k) time to find successors, and then O (z) the total time in all iterations to list all matching pairs.
The asymptotic execution time of this algorithm is good compared to the O (nk) algorithm that you originally posted. Assuming the number of matches is not "really huge" (say, of the order of nk), it will be much faster with increasing n and k.
If the values ββyou store are integers in a small range (say 0 - 10,000), you can speed it up even more by replacing a balanced BST data structure optimized for integers like van Emde Boas tree , which reduces it to O (n log log k + z). Again, this happens faster asymptotically , and if you keep the constant k at 10, it's almost certainly not worth it.
Hope this helps!
source share