본문 바로가기
Algorithm/프로그래머스

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

by 세류오 2023. 5. 22.

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

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

보자마자 Map을 사용해야겠다고 생각이 드는 문제였다.

 

중간에 없는 이름에 대한 예외처리를 생각하지 못하였었다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import java.util.*;
 
class Solution {
    public int[] solution(String[] name, int[] yearning, String[][] photo) {
        int[] answer = new int[photo.length];
 
        //Map으로 풀면 될 것 같은데?
        Map<String, Integer> npList = new HashMap<>();
 
        for(int i = 0; i < name.length; i++) {
            npList.put(name[i], yearning[i]);
        }
 
        int index = 0;
 
        for(String[] names : photo){
            int point = 0;
            //이름이 존재하지 않을 때
            for(int i = 0; i < names.length; i++) {
                if(npList.get(names[i]) == null) {
                    continue;
                } else{
                    point += npList.get(names[i]);
                }
            }
            answer[index] = point;
            index++;
        }
        return answer;
    }
}