- 난이도: Lv1
프로그래머스 링크: https://school.programmers.co.kr/learn/courses/30/lessons/176963
풀이 링크(GitHub): hayannn/CodingTest_Java/프로그래머스/1/176963. 추억점수
풀이 시간 : 14분
입력
계산
출력
import java.util.*;
class Solution {
public int[] solution(String[] name, int[] yearning, String[][] photo) {
int[] answer = new int[photo.length];
//Arrays.stream(photo).distinct().toArray();
int sum = 0;
for(int i=0; i < photo.length; i++){
for(int j=0; j < photo[i].length; j++){
for(int k=0; k < name.length; k++){
if(photo[i][j].equals(name[k])) {
sum += yearning[k];
}
else {
break;
}
}
}
answer[i] = sum;
}
return answer;
}
}
//before
int sum = 0;
for(int i=0; i < photo.length; i++){
for(int j=0; j < photo[i].length; j++){
for(int k=0; k < name.length; k++){
if(photo[i][j].equals(name[k])) {
sum += yearning[k];
}
else {
break;
}
}
}
//after
for(int i=0; i < photo.length; i++){
int sum = 0;
for(int j=0; j < photo[i].length; j++){
for(int k=0; k < name.length; k++){
if(photo[i][j].equals(name[k])) {
sum += yearning[k];
break;
}
}
}
풀이 시간 : 35분(첫 풀이 시간 포함)
입력
계산
출력
import java.util.*;
class Solution {
public int[] solution(String[] name, int[] yearning, String[][] photo) {
int[] answer = new int[photo.length];
//Arrays.stream(photo).distinct().toArray();
//int sum = 0;
for(int i=0; i < photo.length; i++){
int sum = 0;
for(int j=0; j < photo[i].length; j++){
for(int k=0; k < name.length; k++){
if(photo[i][j].equals(name[k])) {
sum += yearning[k];
break;
}
}
}
answer[i] = sum;
}
return answer;
}
}