Algorithm: optimal way to rearrange a list from one order to another?

后端 未结 4 1024
旧巷少年郎
旧巷少年郎 2020-12-30 13:30

EDIT: I\'m not sure that my original question is clear enough. I need an algorithm that will compute the minimal sequence of moves to rearrange an array from one ord

4条回答
  •  星月不相逢
    2020-12-30 14:35

    http://en.wikipedia.org/wiki/Longest_increasing_subsequence

    Find the longest increasing subsequence (according to the new sort order). Then move each element which is not in that sequence, into its place relative to the elements already in the sequence.

    In your example, 'a, b, e' and 'a, c, e' are tied for longest increasing subsequence. The best you can do is choose one of those, and just move the other elements.

提交回复
热议问题