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