알고리즘
프로그래머스 시소 짝궁 (JAVA)
박카스마시며코딩
2023. 4. 12. 12:45
https://school.programmers.co.kr/learn/courses/30/lessons/152996
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
import java.util.*;
class Solution {
public long solution(int[] weights) {
long answer = 0;
Map<Integer,Integer> cnt = new HashMap<>();
for(int weight : weights){
cnt.merge(weight,1,(v1,v2)->{
return v1+1;
});
}
for(int key : cnt.keySet()){
long nowCnt = cnt.getOrDefault(key,0);
if(nowCnt == 0){
continue;
}
answer += nowCnt * (nowCnt - 1) / 2;
int tempCnt = 0;
if(4 * key % 3 == 0){
tempCnt = cnt.getOrDefault((int)((4 * key) / 3),0);
answer += nowCnt * tempCnt;
}
if(3 * key % 2 == 0){
tempCnt = cnt.getOrDefault((int)((3 * key) / 2),0);
answer += nowCnt * tempCnt;
}
tempCnt = cnt.getOrDefault((2*key),0);
answer += nowCnt * tempCnt;
// System.out.println(key + " " + answer);
}
return answer;
}
}