프로그래머스 1) 추억 점수

유병수·2023년 5월 1일
0

추억점수

맵을 사용하는 간단한 문제
처음에는 name을 photoList에서 indexOf로 찾아서 -1이 아니면(존재하면) 점수를 더해주려고 했었다. 하지만 만약 배열이 길어지게 되고 n번째 맨 끝에 존재하게 된다면 불필요한 검색을 많이 할꺼 같아서 map을 사용해 점수를 매핑하고, photo를 순회하면서 점수를 더해지는 방식을 사용했다.

import java.util.*;

class Solution {
    public int[] solution(String[] name, int[] yearning, String[][] photo) {
        // ArrayList<Integer> answer = new ArrayList<>(photo.length);
        int[] answer = new int[photo.length];
        Map<String,Integer> pointMap = new HashMap<>();
        for(int i=0;i<name.length;i++){
            pointMap.put(name[i],yearning[i]);
        }
        
        for(int i=0;i<photo.length;i++){
            for(String findName : photo[i]){
                Integer point = pointMap.get(findName);
                if(point != null){
                    answer[i] += point;
                }
            }
        }
        
        
        return answer;
    }
}

0개의 댓글