코딩 테스트

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

우루쾅 2024. 2. 3. 00:23
728x90
반응형
SMALL

오랜만에 코딩테스트 문제를 풀어봤는데 이렇게 어려웠었나....

 

자주 안쓰던 문법들을 쓰려니 머리가 안돌아갔다(문제는 출처 란에)

 

결국 질문하기에서 풀이 힌트(코드X) 를 참고해서 문제를 풀었다.

 

Map 을 사용해서 String과 Int 를 선언 후 name 과 yearning 값을 각각 선언하여 넣어놨다.

 

그 뒤에 2중 루프를 돌려서 add 값에 점수를 넣어줬는데 에러가 발생?!

 

확인해보니 null 값일 경우 널처리를 안해줘서 에러가 떨어졌다!

if(loof.get(photo[i][j]) == null) continue;

 

널 처리를 한 후 문제 clear~

 

 

 

 

 

 

정답

import java.util.*;

class Solution {
    public int[] solution(String[] name, int[] yearning, String[][] photo) {
        Map<String, Integer> loof = new HashMap<String, Integer>();
        int[] answer = new int[photo.length];
        int add = 0;
        
        for(int i=0; i<name.length; i++){
            loof.put(name[i], yearning[i]);
        }
        
        for(int i=0; i<photo.length; i++){
            for(int j=0; j<photo[i].length; j++){
                if(loof.get(photo[i][j]) == null) continue;
                add += loof.get(photo[i][j]);
            }
            answer[i] = add;
            add = 0;
        }
        
        return answer;
    }
}

 

 

 

 

출처

연습문제 > 추억점수

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

728x90
반응형
LIST