본문 바로가기
코딩테스트

프로그래머스 - 추억 점수

by 어떻게말이름이히힝 2025. 4. 11.

코딩테스트 연습 - 추억 점수 | 프로그래머스 스쿨

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

 

import java.util.*;
class Solution {
    public int[] solution(String[] name, int[] yearning, String[][] photo) {
        Map<String, Integer> score = new HashMap<>();
        
        for(int i = 0; i < name.length; i++) {
            score.put(name[i], yearning[i]);
        }
        
        int[] answer = new int[photo.length];
        
        for(int i = 0; i < photo.length; i++) {
            int sum = 0;
            for(int j = 0; j < photo[i].length; j++) {
                String person = photo[i][j];
                if(score.containsKey(person)) {
                    sum += score.get(person);
                }
            }
            answer[i] = sum;
        }
        
        return answer;
    }
}

// 각 사람의 그리움 점수를 더함
// 사람 name, 점수 yearning / 각 사진에 찍힌 인물의 이름들 photo
// photo에 이중반복문을 써서 이름을 찾고 각 점수를 더해 배열에 저장한다. 
// 이름은 중복될 수 없으니까 Map 사용