[프로그래머스] 176963. 추억점수

기영·2024년 6월 27일

추억점수

문제 https://school.programmers.co.kr/learn/courses/30/lessons/176963

풀이

photo에 등장하는 인물이 name에 있으면 yearning을 모두 더해 결과 배열에 저장한다.

코드

// HashMap 사용

import java.util.*;

class Solution {
    public int[] solution(String[] name, int[] yearning, String[][] photo) {
        Map<String, Integer> hm = new HashMap<>();
        int[] answer = new int[photo.length];
        
        for(int i = 0 ; i < name.length ; i++){
            hm.put(name[i], yearning[i]);
        }
        
        for(int i = 0 ; i < photo.length ; i++){
            int sum = 0;
            for(int j =0 ; j < photo[i].length ; j++){
                if(hm.get(photo[i][j]) != null)
                    sum += hm.get(photo[i][j]);
            }
            answer[i] = sum;
        }
        
        return answer;
    }
}
profile
computer engineering student

0개의 댓글