最大公约数 / 最小公倍数

const gcd = (a, b) => a === 0 ? b : gcd(b%a, a);

const lcm = (a, b) => a * b / gcd(a, b);

数组离散化

数组离散化!(需要去重)

const lower_bound = arr => {
    const map = new Map();
    let save = arr.slice();
    save.sort((a, b) => a-b);
    let count = 0;
    for(const v of save) {
        if(!map.has(v)) {
            map.set(v, count++);
        }
    }
    const ans = [];
    for(const v of arr) {
        ans.push(map.get(v));
    }
    return ans;
}

// test
lower_bound([100, 90, 3,67,89, 89]) // => [ 4, 3, 0, 1, 2, 2 ]
const lower_bound = arr => {
    const map = new Map();   let save = arr.slice(); save.sort((a, b) => a-b);  let count = 0; for(const v of save) {   if(!map.has(v)) { }}const ans = []; for(const v of arr) {   ans.push(map.get(v));} return ans;
}

// test
lower_bound([100, 90, 3,67,89, 89]) // => [ 4, 3, 0, 1, 2, 2 ]
0%