There's another algorithm problem, but I can't solve it.

given two equal-sized arrays An and B, the advantage of An over B can be described by the number of indexes that satisfy A [I] > B [I].

returns an arbitrary arrangement of A to maximize its advantage over B.

example 1:

input: a = [2 br 7, 7, 11, 15], B = [1, 10, 4, 11, 11]
output: [2, 11, 7, 15, 15]
example 2:

input: a = [12 record24, 8, 32], B = [13, 25, 32, 11]
output: [24, 32, 12]

Mar.28,2021

function comp(arr1,arr2){
        var arr=[];
        arr1.sort(function(a,b){
            return a-b;
        });
        arr2.forEach(function(item){
            var index=arr1.findIndex(function(_item){
                return _item>item;
            });
            if(index>-1){
                arr.push(arr1.splice(index,1)[0]);
            }else{
                arr.push(arr1.splice(0,1)[0]);
            }
        });
        return arr;
    }
    console.log(comp([2,7,11,15],[1,10,4,11]));
    console.log(comp([12,24,8,32],[13,25,32,11]));

A = [12, 24, 8, 32], B = [13, 25, 32, 11]
A.sort((a, b) => b - a)
let A1 = [...B].sort((a, b) => b - a).reduce((res, val) => {
  res[B.indexOf(val)] = A[0] > val ? A.shift() : A.pop()
  return res
}, new Array(A.length))
console.log(A1)
Menu