Argorithm

[Programmers]LV.1 추억 점수

빙응이 2023. 11. 28. 20:20


📝풀이

각 사람마다 점수를 저장하고 사진 배열에 따라 점수를 계산해야 한다. 

일반 배열로 실행 시에 배열을 3개나 써야하고 로직이 불편해진다. 

Key, Value 방식으로 {사람: 정수}를 묶어 사용하면 편하게 할 수 있다. 

 

import java.util.HashMap;
class Solution {
    public int[] solution(String[] name, int[] yearning, String[][] photo) {
        HashMap<String, Integer> map = new HashMap<>(); //[A] 해시맵 선언
        int[] answer = new int[photo.length];
        
        for(int i = 0; i < name.length; i++) //[B] 해시맵 초기화
            map.put(name[i], yearning[i]);
        
        for(int i = 0; i < photo.length; i++){
            int count = 0;
            for(int j = 0; j < photo[i].length; j++){ //[C] 배열의 크기에 맞게 총합을 계산
                count += map.getOrDefault(photo[i][j],0);
            }
            answer[i] = count;
        }
        return answer;
    }
}