풀이
파이썬 풀이1 : 리스트와 반복문, 조건문을 사용해서 구현하면 되는 간단한 문제였다.
def solution(name, yearning, photo):
answer = []
for case in photo:
caseSum=0
for people in case:
if people in name:
idx=name.index(people)
caseSum+=yearning[idx]
else:
pass
answer.append(caseSum)
return answer
파이썬 풀이2 : zip함수 이용해서도 해결가능하다.
def solution(name, yearning, photo):
answer = []
year_dict={n:y for n,y in zip(name,yearning)}
ans=0
for pho in photo:
for p in pho:
if p in year_dict.keys():
ans+=year_dict[p]
answer.append(ans)
ans=0
return answer
자바 풀이
import java.util.*;
class Solution {
public int[] solution(String[] name, int[] yearning, String[][] photo) {
int[] answer = new int[photo.length];
for(int idx=0; idx<photo.length; idx++){
int sum=0;
for(String tmp:photo[idx]){
if(Arrays.asList(name).indexOf(tmp)!=-1){
sum+=yearning[Arrays.asList(name).indexOf(tmp)];
}
}
answer[idx]=sum;
}
return answer;
}
}
'알고리즘' 카테고리의 다른 글
프로그래머스 혼자서 하는 틱택토 (0) | 2023.04.05 |
---|---|
BOJ 15787번(기차가 어둠을 헤치고 은하수를) (0) | 2023.04.05 |
BOJ 1354번(무한 수열2) (0) | 2023.04.04 |
BOJ 9742번 - 순열 (0) | 2023.04.04 |
BOJ 10610번(30) (0) | 2023.04.02 |