programmers_시소 짝꿍_java
2023. 5. 18. 19:18ㆍAlgorithm/Programmers
728x90
https://school.programmers.co.kr/learn/courses/30/lessons/152996
import java.util.*;
class Solution {
public long solution(int[] weights) {
long answer = 0;
Arrays.sort(weights);
Map<Double, Integer> map = new HashMap<>();
for (int weight : weights){
answer += isPair(weight, map);
}
return answer;
}
public int isPair(int weight, Map<Double, Integer> map){
int res = 0;
// 자신 보다 작거나 같은 페어의 경우의 수 4가지
double case1 = weight * 1.0;
double case2 = (weight * 1.0 )/ 2.0;
double case3 = (weight * 2.0 )/ 3.0;
double case4 = (weight * 3.0 )/ 4.0;
if (map.containsKey(case1)){
res += map.get(case1);
}
if (map.containsKey(case2)){
res += map.get(case2);
}
if (map.containsKey(case3)){
res += map.get(case3);
}
if (map.containsKey(case4)){
res += map.get(case4);
}
map.put(case1, map.getOrDefault(case1, 0) + 1);
return res;
}
}
'Algorithm > Programmers' 카테고리의 다른 글
programmers_우박수열 정적분_java (1) | 2023.05.21 |
---|---|
programmers_N-Queen_java (0) | 2023.05.19 |
programmers_테이블 해시 함수_java (0) | 2023.05.17 |
programmers_후보키_java (0) | 2023.05.17 |
programmers_혼자 놀기의 달인_java (0) | 2023.05.15 |