https://school.programmers.co.kr/learn/courses/30/lessons/258712
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
1레벨짜리 문제가 벌써 풀기 싫어진다.
매번 프로그래머스에서 풀때마다 느끼지만 1~2레벨 구분은 별 의미도 없는것 같다..
우선 처음 고려해야 할 것은 서로 주고받은 선물의 개수이다.
두 번째는 선물지수이다.
이름을 키로 갖고 값을 선물 지수로 갖는 map,
서로 선물을 주고받은 데이터를 저장하는 map 두개가 필요하다.
서로 주고받은 선물 개수가 같은 것과 둘다 주고받지 않은것은 같다고 볼 수 있다.
import java.util.HashMap;
import java.util.Map;
class Solution {
public int solution(String[] friends, String[] gifts) {
int answer = 0;
Map<String, Integer> giftScoreMap = new HashMap<>();
Map<String, Map<String, Integer>> giveAndTakeMap = new HashMap<>();
for (int i = 0; i < friends.length; i++) {
giftScoreMap.put(friends[i], 0);
}
for (int i = 0; i < friends.length; i++) {
Map<String, Integer> giftScoreMap2 = new HashMap<>(giftScoreMap);
giveAndTakeMap.put(friends[i], giftScoreMap2);
giveAndTakeMap.get(friends[i]).remove(friends[i]);
}
for (int i = 0; i < gifts.length; i++) {
String[] split = gifts[i].split(" ");
giftScoreMap.put(split[0], giftScoreMap.getOrDefault(split[0], 0) + 1);
giftScoreMap.put(split[1], giftScoreMap.getOrDefault(split[1], 0) - 1);
Map<String, Integer> takeMap = giveAndTakeMap.get(split[0]);
takeMap.put(split[1], takeMap.getOrDefault(split[1], 0) + 1);
}
for (int i = 0; i < friends.length; i++) {
int highScore = 0;
for (String score : giveAndTakeMap.get(friends[i]).keySet()) {
if (giveAndTakeMap.get(friends[i]).get(score) > giveAndTakeMap.get(score).get(friends[i])) {
highScore++;
} else if (
giveAndTakeMap.get(friends[i]).get(score) == giveAndTakeMap.get(score).get(friends[i])) {
if(giftScoreMap.get(friends[i]) > giftScoreMap.get(score)) {
highScore++;
}
}
}
answer = Math.max(answer, highScore);
}
return answer;
}
}
'알고리즘 > Lv1. 프로그래머스' 카테고리의 다른 글
같은숫자는싫어 (0) | 2023.07.17 |
---|---|
체육복 (0) | 2023.07.17 |
공원 산책 (0) | 2023.07.13 |
개인정보 수집 유효기간 (0) | 2023.07.13 |
2016년 (0) | 2023.07.11 |