dlehdanakf / Codingtest-Study

알고리즘 코딩테스트 토막지식 정리
1 stars 0 forks source link

Greedy #7

Open dlehdanakf opened 4 years ago

dlehdanakf commented 4 years ago

개요

예제

섬 연결하기

function solution(n, costs) {
    const nodes = new Array(n).fill(false);
    const sorted = costs.sort((a, b) => a[2] - b[2]);

    let answer = 0;
    for(let i = 0; i < sorted.length; i++) {
        const [ n1, n2, w ] = sorted[i];
        if(nodes[n1] === false || nodes[n2] === false) {
            nodes[n1] = nodes[n2] = true;
            answer += w;

            if(nodes.every(e => e === true)) {
                break;
            }
        }
    }

    return answer;
}
function solution(n, costs) {
    const groups = new Array(n).fill(undefined).map((_, i) => [i]);
    const sorted = costs.sort((a, b) => a[2] - b[2]);

    let answer = 0;
    for(const [ n1, n2, w ] of sorted) {
        const n1_i = groups.findIndex(g => g.includes(n1));
        const n2_i = groups.findIndex(g => g.includes(n2));
        if(n1_i === n2_i) {
            continue;
        }

        groups[n1_i] = [...groups[n1_i], ...groups[n2_i]];
        groups[n2_i].length = 0;
        answer += w;
    }

    return answer;
}
dlehdanakf commented 4 years ago

섬 연결하기 예전코드

function build_bridge(connected_group, from, to) {
    /* if is duplicated */
    for(const group of connected_group) {
        if(group.includes(from) && group.includes(to)) {
            return false;
        }
    }

    let _from_group_index = null, _to_group_index = null;
    for(const i in connected_group) {
        if(connected_group[i].includes(from)) { _from_group_index = i; }
        if(connected_group[i].includes(to)) { _to_group_index = i; }
    }

    if(_from_group_index === null && _to_group_index === null) {
        /* if is new group */
        connected_group.push([ from, to ]);
    } else if(_from_group_index !== null && _to_group_index !== null) {
        /* if is connecting group */
        connected_group[_from_group_index] = connected_group[_from_group_index].concat(connected_group[_to_group_index]);
        connected_group.splice(_to_group_index, 1);
    } else if(_from_group_index !== null) {
        /* if is `from` connected */
        connected_group[_from_group_index].push(to);
    } else {
        /* if is `to` connected */
        connected_group[_to_group_index].push(from);
    }

    return true;
}
function solution(n, costs) {
    const c = costs
        .map((v, i) => {
            const [ from, to, cost ] = v;
            return { from, to, cost };
        })
        .sort((a, b) => { return a.cost < b.cost ? -1 : a.cost > b.cost ? 1 : 0; });

    let answer = 0;
    const connected_group = [];
    while(c.length > 0) {
        const [{ from, to, cost }] = c.splice(0, 1);
        const result = build_bridge(connected_group, from, to);
        if(result === true) {
            answer += cost;
        }

        if(connected_group.length > 0 && connected_group[0].length === n) {
            break;
        }
    }

    return answer;
}