[프로그래머스] 시소 짝꿍 Lv1

2025. 2. 12. 19:08코딩 테스트 준비

728x90
반응형

https://school.programmers.co.kr/learn/courses/30/lessons/152996?language=javascript

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

 

function solution(weights) {
  const map = {};
  const ratio = [1, 3 / 2, 4 / 3, 2]; // 비율 배열

  // 무게를 내림차순 정렬 후 reduce로 합산
  return weights
    .sort((a, b) => b - a)
    .reduce((result, weight) => {
      // 해당 무게의 비율을 곱하여 result에 합산
      ratio.map((v) => (result += map[weight * v] || 0)); 
    
      // 해당 무게의 등장 횟수 1 증가
      map[weight] = (map[weight] || 0) + 1; 
      return result;
    }, 0);
}

 

728x90
반응형