Computer/Algorithm_Java
[Algorithm_Java] 추억 점수 (Success)
HJ0216
2023. 10. 20. 11:03
728x90
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
☕Language: Java
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
32
33
34
|
import java.util.Map;
import java.util.HashMap;
class Solution {
static int[] answer;
static Map<String, Integer> yearningScore;
public int[] solution(String[] name, int[] yearning, String[][] photo) {
answer = new int[photo.length];
yearningScore = new HashMap<>();
for(int i=0; i<name.length; i++){
yearningScore.put(name[i], yearning[i]);
}
int index=0;
for(String[] peopleInPhoto : photo){
answer[index++] = calcYearningScore(peopleInPhoto);
}
return answer;
}
public int calcYearningScore(String[] peopleInPhoto){
int sumYearning = 0;
for(String personInPhoto : peopleInPhoto){
if(yearningScore.get(personInPhoto)!=null)
sumYearning += yearningScore.get(personInPhoto);
}
return sumYearning;
}
}
|
1. yearningScore = new HashMap<>();
- key: 인물, value: 그리움 점수
2. answer[index++] = calcYearningScore(peopleInPhoto);
- 그리움 점수 계산
- if(yearningScore.get(personInPhoto)!=null)
- 사진 속 그리움 대상 인물이 아니라면, yearningScore로 부터 그리움 점수 합산
😮 찾아본 풀이
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
class Solution {
public int[] solution(String[] name, int[] yearning, String[][] photo) {
int[] answer = new int[photo.length];
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])) answer[i] += yearning[k];
}
}
}
return answer;
}
}
|
* 3중 for문
- photo의 각 인물과 name 배열 비교
🔗 소스 코드
GitHub
📚 참고 자료
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
728x90