슈콩

[프로그래머스] Lv.2 시소 짝꿍 본문

Algorithms/Programmers

[프로그래머스] Lv.2 시소 짝꿍

shukong 2025. 10. 22. 00:03

 

 

[문제]

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

 

프로그래머스

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

programmers.co.kr

 

 

[소스 코드]

import java.util.*;
class Solution {
    public long solution(int[] weights) {
        long answer = 0;
        Arrays.sort(weights);
        Map<Double,Integer> hm = new HashMap<>();
        for(int w : weights){
            double a = w * 1.0;
            double b = w * 2.0/3.0;
            double c = w * 2.0/4.0;
            double d = w * 3.0/4.0;
            
            if(hm.containsKey(a)) answer += hm.get(a);
            if(hm.containsKey(b)) answer += hm.get(b);
            if(hm.containsKey(c)) answer += hm.get(c);
            if(hm.containsKey(d)) answer += hm.get(d);
            
            hm.put(a,hm.getOrDefault(a,0)+1);
        }
        return answer;
    }
}