-
추억 점수알고리즘/프로그래머스 2023. 5. 11. 10:32
문제 링크
https://school.programmers.co.kr/learn/courses/30/lessons/176963
해설
길이가 많이 길지 않으므로 O(N^2)로 편하게 풀면 된다
코드
import java.util.*; class Solution { public int[] solution(String[] name, int[] yearning, String[][] photo) { Map<String, Integer> nameYearingMap = new HashMap<>(); int size = name.length; for (int i = 0; i < size; ++i) { nameYearingMap.put(name[i], yearning[i]); } List<Integer> answer = new ArrayList<>(); for (int i = 0; i < photo.length; ++i) { int sum = 0; for (int j = 0; j < photo[i].length; ++j) { Integer result = nameYearingMap.get(photo[i][j]); if (null != result) { sum += nameYearingMap.get(photo[i][j]); } } answer.add(sum); } return answer.stream().mapToInt(i -> i).toArray(); } }
'알고리즘 > 프로그래머스' 카테고리의 다른 글
공원 산책 (0) 2023.05.11 [Java] 달리기 경주 (0) 2023.05.03 [프로그래머스] 순위 (0) 2021.09.16 [프로그래머스] 정수 삼각형 (0) 2021.09.15 [프로그래머스] 디스크 컨트롤러 (0) 2021.09.15