javascript - Convert one array to another with minimal quantity of splices -


Let's, for example, this array

  ar = [6,3,5] ], 1,2]   

I want to convert it to another array and I can only use two operations - special status (insert (i, 0, item) Item) or remove items from the specific position (arm (i, 1)) I am looking for a solution that uses the least amount of diameter.

The second important condition is that we consider arrays with unique values, there are no doubles in our array.

For example,

  ar1 = [6,3,10,5,1,2]; Ar2 = [6,3,1,2,5]; It is clear that if we want to get RE from R1, we only need one piece - AR.splice (2,0,10). If we want to get 2 ar 2, then we need to splice two: ar.splice (2,1) and then push (5) (second is equal to benzene (ar.length, 0,5))  

By the way, this work is a natural practical value. Let's imagine, for example, a list of products and product filters. We change the filter settings and the list respectively. And every change after beauty slides jquery slow - slide down animation This animation can slide up and hide specific items or put a new slide. This task is to reduce the amount of these animation animations. This means that we try to reduce the amount of dome-manipulation of the list.

I hope the code is sorted out. This code is somehow based on the Livenshitin distance concept. This is very useful for this problem, as mentioned in the answer to the manic. For simplicity, I have used Erase and Python instead of stromes.
It seems that the basic problem easily reduces the same problem for two arrays, because of the equal length of the same set of integers, I assumed that the initial string and the target string have the same length and in this There is only one set of characters.
Python Code:

  random import # Create random initials and target (strout) strings s = "abcdefghijklmnopqrstuvwxyz" l = list (s) random.shuffle (l) strout = '' .join (l) random.shuffle (l) strin = '' .join (l) # its use test #strin = "63125798" #true = "63512897" (len (strin) -1, -1 , -1) Strain, Straw Ins_del = 0 Print: If String [i]! = Straw [i]: If strin [i-1] == ll [i]: ii = strout.find (strin [i], 0, i) strin = strin [: ii] + strin [i] + strin [ Ii: i] + Strin [i + 1:] ins_del = ins_del + 1 #Test Output Print "1:", strin else: ii = strin.find (strout [i], 0, i-1) strin = strin [ : Ii] + strin [ii + 1: i + 1] + strout [i] + strin [i + 1:] ins_del = ins_del + 1 # test output print "2:", string print string, strout # 0, lane (Strin): If strin [i]! = Straw [i]: Print "In Error", I, "-the Symbol" Print "Enter / Dealit Operation =", ins_del   

Production example:

  Kevlkeeprdafmovtrjbtzrkuskson Kjokmigphbuaztelvvfrsdnsi 2: Kevlkeeprdafmovgajbtzrkusshny 1: Kevlkeeprdafmovgajbtzkusngyr 2: Kevlkeeprhfmvgajbtzrkusdansi 2: Kevlsiffmovgajbtzrkursdnsi 2: Kevlsifmovgajbtzrkufarasdnsi 2: Kevlkeephmogajbtzrkuwfrsdnsi 2: Kailsifmogajbtzrkuwvfrsdnsi 2: Kesifmogajbtzrkulvwfrsdnsi 2: Ksifmogajbtzkelvvfrsdnsi 2: Ksifmogajbakutelvvfrsdnsi 2: Ksifmogajbkuztlwvfrsdnsi 2: Ksifmogajbkatelvvfrsdnsi 1: Kksifmogajbatelvvfrsdnsi 2: Kkspmogjbatelvvfrsdnsi 2: Qkcimogjphbuaztelwvfrsdnxy 1: qjkcimogphbuaztelwvfrsdnxy 2: qjkcmoigphbuaztelwvfrsdnxy 1: qjokcmigphbuaztelwvfrsdnxy 1: qjockmigphbuaztelwvfrsdnxy qjockmigphbuaztelwvfrsdnxy qjockmigphbuaztelwvfrsdnxy included Do / Delay Operations = 19    

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -