File tree 1 file changed +46
-0
lines changed
1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[] } target
3
+ * @param {number[] } arr
4
+ * @return {number }
5
+ */
6
+ const minOperations = function ( target , arr ) {
7
+ let length1 = target . length ,
8
+ length2 = arr . length
9
+ const targetMap = new Map ( )
10
+ for ( let i = 0 ; i < length1 ; i ++ ) targetMap . set ( target [ i ] , i )
11
+ const list = new Array ( )
12
+ for ( let i = 0 ; i < length2 ; i ++ ) {
13
+ let num = arr [ i ]
14
+ if ( targetMap . has ( num ) ) list . push ( targetMap . get ( num ) )
15
+ }
16
+ let longestIncreasing = lengthOfLIS ( list )
17
+ return target . length - longestIncreasing
18
+
19
+ function lengthOfLIS ( list ) {
20
+ let length = 1 ,
21
+ size = list . length
22
+ if ( size == 0 ) return 0
23
+ const d = new Array ( size + 1 ) . fill ( 0 )
24
+ d [ length ] = list [ 0 ]
25
+ for ( let i = 1 ; i < size ; ++ i ) {
26
+ if ( list [ i ] > d [ length ] ) {
27
+ d [ ++ length ] = list [ i ]
28
+ } else {
29
+ let left = 1 ,
30
+ right = length ,
31
+ pos = 0
32
+ while ( left <= right ) {
33
+ let mid = ( left + right ) >> 1
34
+ if ( d [ mid ] < list [ i ] ) {
35
+ pos = mid
36
+ left = mid + 1
37
+ } else {
38
+ right = mid - 1
39
+ }
40
+ }
41
+ d [ pos + 1 ] = list [ i ]
42
+ }
43
+ }
44
+ return length
45
+ }
46
+ }
You can’t perform that action at this time.
0 commit comments