'What is the most efficient way of finding the longest increasing subsequence (sorted string) in a circular buffer?

Since the sequence is in a circular buffer, a sequence of 1 2 3 4 would be thought the same as 2 3 4 1, 3 4 1 2, or 4 1 2 3, depending on the starting index.

In this context, what I am trying to find is this:

Example 1

In 3 4 1 2 5, the longest increasing subsequence would be 1 2 3 4, not 1 2 5. Of course, this would be easy to find if the sequence started at 1.

Example 2

In 1 4 5 2 3, the longest increasing subsequence would be 2 3 4 5, not 1 2 3. This again would be easy to find if the sequence started at 2.

Example 3

The sequence 3 4 5 1 2 would be considered sorted, and its LIS would be 1 2 3 4 5.

I am trying to find an algorithm that does this, but all I could find is finding the longest increasing subsequence from a finite sequence, resulting in a shorter subsequence as in the examples. The optimal algorithm of finding LIS from a finite sequence seems to be of O(n log n) as illustrated in https://en.wikipedia.org/wiki/Longest_increasing_subsequence#Efficient_algorithms, but the naive way of applying this to my problem would grow to O(n^2 log n), starting over at every number (or letter). Is there a better way of doing this?

I am sorry if I have used the wrong terminologies that might cause confusion.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source