본문 바로가기
Coding Test/Programmers

[Programmers] Lv 2. 시소 짝꿍

by song.ift 2023. 4. 6.

https://school.programmers.co.kr/learn/courses/30/lessons/152996

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

function solution(weights) {
    let count = 0;

    const dp = weights.reduce((obj, el) => {
        if (obj[el]) count += obj[el]; // 중복되는 거리에 있는 것들 카운팅.
        obj[el] = ++obj[el] || 1;
        return obj;
    }, {});

    const [arr, itvDp] = [Object.keys(dp), {}];

    // 2 ~ 4 긴갹에 있는 것들 체크 (곱한것을 누적하는 형식)
    for (let i = 2; i <= 4; ++i) {
        arr.forEach((el) => {
            const val = i * el;

            if (itvDp[val]) count += dp[el] * itvDp[val];
            itvDp[val] = itvDp[val] + dp[el] || dp[el];
        });
    }

    return count;
}

GitHub : https://github.com/developeSHG/Algorithm-Baekjoon_Programmers/blob/main/%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%A8%B8%EC%8A%A4/unrated/152996.%E2%80%85%EC%8B%9C%EC%86%8C%E2%80%85%EC%A7%9D%EA%BF%8D/%EC%8B%9C%EC%86%8C%E2%80%85%EC%A7%9D%EA%BF%8D.js

 

GitHub - developeSHG/Algorithm-Baekjoon_Programmers: 백준 and 프로그래머스 소스코드

백준 and 프로그래머스 소스코드. Contribute to developeSHG/Algorithm-Baekjoon_Programmers development by creating an account on GitHub.

github.com

 

댓글