@@ -19,49 +19,53 @@ const makeArrayIncreasing = (arr1, arr2) => {
19
19
// sortedArr2.push(firstPartOfArr2.shift());
20
20
// }
21
21
// }
22
- let count = 0 , tmpPrev = null ;
22
+ let count = 0 , tmpnext = null ;
23
23
for ( let i = 0 ; i < arr1 . length ; i ++ ) {
24
- let prev = arr1 [ i - 1 ] ;
25
- if ( undefined === prev || undefined === arr1 [ i - 2 ] ) continue ;
24
+ let next = arr1 [ i + 1 ] ;
25
+ if ( undefined === next || undefined === arr1 [ i + 2 ] ) continue ;
26
26
27
- console . log ( `prev:${ prev } > arr1[i]:${ arr1 [ i ] } ` )
28
- if ( prev > arr1 [ i ] ) {
29
- let tmpCount = 0 ;
27
+ console . log ( `-> i:${ i } , curr:${ arr1 [ i ] } - next:${ next } < arr1[i]:${ arr1 [ i ] } ` )
28
+ if ( next < arr1 [ i ] ) {
30
29
31
30
// Get smallest
32
- let toReplace = [ ] , smallest = Infinity ;
31
+ let smallest = Infinity , smallestIdx = - 1 ;
33
32
for ( let j = 0 ; j < arr2 . length ; j ++ ) {
34
- console . log ( `j:${ j } ` )
35
- console . log ( arr2 )
36
- console . log ( `> ? ${ arr1 [ i - 2 ] } < ${ arr2 [ j ] } < ${ arr1 [ i ] } - tmpPrev:${ tmpPrev } , arr2[j]:${ arr2 [ j ] } ` )
37
- if ( tmpPrev === arr2 [ j ] ) {
38
- console . log ( "ENd" )
33
+ console . log ( `-> j:${ j } , arr2[j]:${ arr2 [ j ] } ` )
34
+
35
+ if ( tmpnext === arr2 [ j ] ) {
36
+ console . log ( "ENd ..." )
39
37
return - 1 ;
40
38
}
41
- if ( arr1 [ i - 2 ] < arr2 [ j ] && arr2 [ j ] < arr1 [ i ] ) {
42
- // tmpCount++;
43
- // let btwItem = arr2[j];
44
- // arr2.splice(j, 1)
45
- // tmpPrev = btwItem;
46
- // console.log(`> Y ${arr1[i - 2]} < ${arr2[j]} < ${arr1[i + expand]}`)
47
- // arr1[i - 1] = btwItem;
48
- // console.log(arr1)
49
- toReplace . push ( arr2 [ j ] )
50
- smallest = Math . min ( smallest , arr2 [ j ] ) ;
39
+
40
+ // if (arr1[i - 2] < arr2[j] && arr2[j] < arr1[i] && arr2[j] < smallest) {
41
+ if ( arr1 [ i - 1 ] < arr2 [ j ] && arr2 [ j ] < smallest ) {
42
+ console . log ( `---> ${ arr1 [ i - 1 ] } < [${ arr2 [ j ] } ] < ${ smallest } ` )
43
+ smallest = arr2 [ j ] ;
44
+ smallestIdx = j ;
51
45
}
46
+ }
47
+
52
48
53
- count += tmpCount ;
54
- console . log ( "toReplace" )
55
- console . log ( toReplace )
56
- console . log ( "arr1" )
57
- console . log ( arr1 )
58
- console . log ( "smallest" )
59
- console . log ( smallest )
60
- arr1 [ i - 1 ] = smallest ;
49
+ if ( Infinity !== smallest ) {
50
+ console . log ( "smallest:" + smallest )
51
+ console . log ( "smallestIdx:" + smallestIdx )
52
+ arr2 . splice ( smallestIdx , 1 ) ;
53
+ arr1 [ i ] = smallest ;
54
+ count ++ ;
55
+ // i--;
61
56
}
57
+
58
+ console . log ( "arr1:post" )
59
+ console . log ( arr1 )
60
+
61
+ console . log ( "> arr2:post" )
62
+ console . log ( arr2 )
62
63
}
63
64
}
64
65
66
+ console . log ( )
67
+
68
+
65
69
return ( 0 === count ) ? - 1 : count ;
66
70
} ;
67
71
@@ -70,7 +74,8 @@ let x = null;
70
74
// x = makeArrayIncreasing([1, 5, 3, 6, 7], [4, 3, 1]);
71
75
// x = makeArrayIncreasing([1, 5, 3, 6, 7], [1, 6, 3, 3]);
72
76
// x = makeArrayIncreasing([7, 6, 3, 0, 3], [9]);
73
- x = makeArrayIncreasing ( [ 0 , 11 , 6 , 1 , 4 , 3 ] , [ 5 , 4 , 11 , 10 , 1 , 0 ] ) ;
77
+ x = makeArrayIncreasing ( [ 0 , 11 , 6 , 1 , 4 , 3 ] , [ 5 , 4 , 11 , 10 , 1 , 0 ] ) ; // 5
78
+ x = makeArrayIncreasing ( [ 0 , 10 , 20 , 30 , 4 ] , [ 1 , 2 , 3 ] ) ; // 5
74
79
75
80
console . log ( "Result" ) ;
76
81
console . log ( x ) ;
0 commit comments