csct3434
[level 2] 시소 짝꿍 - 152996 본문
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
class Solution {
public long solution(int[] weights) {
long answer = 0;
HashMap<Integer, Integer> countPerWeight = new HashMap<>();
HashMap<Integer, List<Integer>> torqueMap = new HashMap<>();
for (int k : weights) {
countPerWeight.put(k, countPerWeight.getOrDefault(k, 0) + 1);
}
answer += countPerWeight.values().stream().mapToLong(x -> (long) x * (x - 1) / 2).sum();
for (int weight : countPerWeight.keySet()) {
for (int distance = 2; distance <= 4; distance++) {
int torque = weight * distance;
if (!torqueMap.containsKey(torque)) {
torqueMap.put(torque, new LinkedList<>());
}
torqueMap.get(torque).add(weight);
}
}
for (int torque : torqueMap.keySet()) {
List<Integer> weight = torqueMap.get(torque);
for (int i = 0; i < weight.size(); i++) {
for (int j = i + 1; j < weight.size(); j++) {
answer += (long) countPerWeight.get(weight.get(i)) * countPerWeight.get(weight.get(j));
}
}
}
return answer;
}
}
'프로그래머스' 카테고리의 다른 글
[level 3] 디스크 컨트롤러 - 42627 (0) | 2024.03.17 |
---|---|
[level 3] 베스트앨범 - 42579 (0) | 2024.03.17 |
[level 2] 테이블 해시 함수 - 147354 (0) | 2024.03.07 |
[level 2] 교점에 별 만들기 - 87377 (1) | 2024.03.07 |
[level 3] 억억단을 외우자 - 138475 (0) | 2024.03.06 |