보라코딩

프로그래머스 자바 :: 추억점 본문

프로그래머스 (java)

프로그래머스 자바 :: 추억점

new 보라 2024. 5. 25. 21:42

HashMap이 바로 생각나서 쉽게 금방 풀었다!

 

 

map.containsKey(...) 를 사용하는 법도 배워 가자!

 if(map.containsKey(person)){

 

 

 

 

import java.util.*;

class Solution {
    public int[] solution(String[] name, int[] yearning, String[][] photo) {
int[] answer = new int[photo.length];
        
        HashMap<String, Integer> map = new HashMap<>();
        for (int i = 0; i < name.length; i++) {
            map.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(map.get( photo[i][j] ) != null) {
                    sum += map.get( photo[i][j] );
                }
            }
            answer[i] = sum;  
        }
        
        return answer;
    }
}