단순히 문제조건에 따라 구현하는 문제이다.
Lv.1 단계라서 어렵지는 않았다.
table = [
[1, 0, "R"],
[1, 0, "T"],
[2, 0, "C"],
[2, 0, "F"],
[3, 0, "J"],
[3, 0, "M"],
[4, 0, "A"],
[4, 0, "N"],
]
def solution(survey, choices):
point = [0, 3, 2, 1, 0, 1, 2, 3]
for i in range(len(survey)):
p = point[choices[i]]
if choices[i] < 4:
plus(survey[i][0], p)
else:
plus(survey[i][1], p)
table.sort()
answer = ''
for i in range(0, 8, 2):
answer += table[i][2]
return answer
def plus(w, p):
for lst in table:
if lst[2] == w:
lst[1] -= p
import java.util.*;
class Solution {
static Node[] table ;
static int[] point = new int[]{0, 3, 2, 1, 0, 1, 2, 3};
public String solution(String[] survey, int[] choices) {
table = new Node[8];
table[0] = new Node(1, 0, "R");
table[1] = new Node(1, 0, "T");
table[2] = new Node(2, 0, "C");
table[3] = new Node(2, 0, "F");
table[4] = new Node(3, 0, "J");
table[5] = new Node(3, 0, "M");
table[6] = new Node(4, 0, "A");
table[7] = new Node(4, 0, "N");
for (int i = 0; i < survey.length; i ++) {
int idx = choices[i];
int p = point[idx];
if (idx < 4) {
String w = String.valueOf(survey[i].charAt(0));
plus(w, p);
} else {
String w = String.valueOf(survey[i].charAt(1));
plus(w, p);
}
}
Arrays.sort(table, new Comparator<Node>() {
@Override
public int compare(Node o1, Node o2) {
if (o1.num != o2.num) {
return o1.num - o2.num;
}
if (o1.point != o2.point) {
return o1.point - o2.point;
}
return o1.word.compareTo(o2.word);
}
});
String answer = "";
for (int i = 0; i < 8; i+=2) {
answer += table[i].word;
}
return answer;
}
static class Node {
int num;
int point;
String word;
public Node(int num, int point, String word) {
this.num = num;
this.point = point;
this.word = word;
}
}
static void plus(String w, int p) {
for (Node node : table) {
if (node.word.equals(w)) {
node.point -= p;
}
}
}
}
MBTI 처럼 유형의 순서가 정해져있다.
따라서 table에 검사의 점수를 계산하고 마지막에 table을 정렬함으로서 필요한 부분만 뽑아낼 것이다.
x
점 얻었을 때 table에는 -x
를 더해줬는데 그 이유는 table을 오름차순 정렬해서 사용할 것이기 때문에 음수값을 더 해주면서 정렬이 올바르게 되도록 했다.-
을 붙여 더해준 이유가 여기있는데 정렬시에 값이 같다면 사전순을 빠른순이 높은 점수를 얻는 것과 같도록 정렬되야하기에 그렇다.tabler값의 정렬순서는 인덱스 0, 인덱스 1, 인덱스 2 순서이다.
보통을 key = lambda x ~
를 통해 정렬 조건을 잡아주는데 이 경우는 인덱스 순서이기 때문에 필수는 아니다.
설문지 지표의 점수를 인덱스로 매칭한 리스트이다.
w 성격 유형에 -p점을 더한다.
w 해당원소를 바로 찾을 수 없어서 완전탐색을 거쳤다.
table이 정렬 되면 1순위는 성격유형(table[i][0]
) 이기 때문에 한 칸씩 띄어서 추가해주면 되겠다.
for i in range(0, 8, 2)