어느 공원 놀이터에는 시소가 하나 설치되어 있습니다. 이 시소는 중심으로부터 2(m), 3(m), 4(m) 거리의 지점에 좌석이 하나씩 있습니다.
이 시소를 두 명이 마주 보고 탄다고 할 때, 시소가 평형인 상태에서 각각에 의해 시소에 걸리는 토크의 크기가 서로 상쇄되어 완전한 균형을 이룰 수 있다면 그 두 사람을 시소 짝꿍이라고 합니다. 즉, 탑승한 사람의 무게와 시소 축과 좌석 간의 거리의 곱이 양쪽 다 같다면 시소 짝꿍이라고 할 수 있습니다.
사람들의 몸무게 목록 weights이 주어질 때, 시소 짝꿍이 몇 쌍 존재하는지 구하여 return 하도록 solution 함수를 완성해주세요.
import java.util.*;
class Solution {
public long solution(int[] weights) {
Map<Double, Integer> hm = new HashMap<>();
long ret = 0;
Arrays.sort(weights);
for(int weight : weights) {
ret += helper(weight, hm);
}
return ret;
}
public long helper(int w, Map<Double, Integer> hm) {
long ret = 0;
double d1 = w*1.0;
double d2 = (w*2.0)/3.0;
double d3 = (w*1.0)/2.0;
double d4 = (w*3.0)/4.0;
if(hm.containsKey(d1)) ret += hm.get(d1);
if(hm.containsKey(d2)) ret += hm.get(d2);
if(hm.containsKey(d3)) ret += hm.get(d3);
if(hm.containsKey(d4)) ret += hm.get(d4);
hm.put(w*1.0, hm.getOrDefault(w*1.0, 0)+1);
return ret;
}
}