Please give us the real problem, then, and not a "fake" small example.
I'm suspicious that this problem is of the order O(2**N). If so, it's going to be "really" slow
on "super long" sets.
| [reply] |
I'm suspicious that this problem is of the order O(2**N).
I suspect it is more like O(N!).
I also suspect that whilst inspecting the longest possible combinations for monotonicity, then in order of decreasing length so that you can short-circuit the full search space, sounds like an optimisation; in reality it will end up inspecting many more possibilities than a brute-force forward search with short-circuiting of the inner iterations.
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] |
I believe this can be reduced to LCS which is O(much better). Diff'ing the original data with its ordered copy would yield a sequence that (1) belongs to the original sequence, (2) is ordered and (3) is among the longest such sequences. (Answered below with code example).
| [reply] |
gilthoniel, you should be able to figure out how to use tybalt89's code as a starting point, but stopping as soon as you have a match. If you test the length of @answers at the end of the while loop, you can stop right away.
Or you could add a similar check to GrandFather's answer — though, not having run his, I'm not positive his will run in longest-first order.
Or, if the one to four lines of code (depending on how Perl-ly you think) that would be required to exit out of the loop when there's at least one element in @answers — if that's too difficult, you could look at my answer, which already tells you the one character you need to edit in order to get it to stop at the first match rather than listing them all. Given that my answer starts with the longest possible combination, and works its way down (inspired by your own structure, btw, to try to make it as easy for you to understand as possible), the first match will be the longest (or the first of the group that are all the same longest length, if there are multiple answers of the same length). If that won't work for you, please describe what feature of the answer does not meet your requirements.
| [reply] [d/l] [select] |