티스토리 뷰

728x90

https://school.programmers.co.kr/learn/courses/30/lessons/118666

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

2022 KAKAO TECH INTERNSHIP의 Level 1 문제다.

 

 

문제 자체는 어렵진 않은데 문제를 이해하는데 시간을 꽤나 썼던 것 같다.

 

먼저 성격 유형과 성격 유형의 점수를 나타내는 Node 클래스를 생성하고,

점수 0을 가지는 모든 성격 유형의 Node를 ArrayList에 저장한다.

 

그리고 choice에서 어떤 값을 선택했는지에 따라 성격 유형 Node의 점수를 더한다.

이 때 각 선택에 따른 점수를 저장해둔 score배열을 통해 합산한다.

그 후 유형별 점수에 따라 유형 4개를 선택하면 된다.

import java.util.ArrayList;
import java.util.HashMap;

class Solution {
    
    	static class Node {
		char type;
		int score = 0;

		Node(char type) {
			this.type = type;
		}

		void addScore(int score) {
			this.score += score;
		}
	}
    
    public String solution(String[] survey, int[] choices) {
		int[] score = {0, 3, 2, 1, 0, 1, 2, 3};
		char[] type = {'A', 'R', 'T', 'C', 'F', 'J', 'M', 'A', 'N'}; // 인덱스 0 무시
		HashMap<Character, Integer> map = new HashMap<>();

		for(int i=0;i<type.length;i++)
			map.put(type[i], i);

		ArrayList<Node> list[] = new ArrayList[5];

		for(int i=1;i<5;i++) {
			list[i] = new ArrayList<>();
			list[i].add(new Node(type[i * 2 - 1]));
			list[i].add(new Node(type[i * 2]));
		}

		for(int i=0;i<survey.length;i++) {
			int idx1, idx2;
			char t;
			int c = choices[i];

			if(c < 4) 
				t = survey[i].charAt(0);
			else if(c == 4) 
				continue;
			else 
				t = survey[i].charAt(1);

			int tmp = map.get(t);
			idx1 = tmp % 2 == 0 ? tmp / 2 : (tmp + 1) / 2;
			idx2 = tmp % 2 == 0 ? 1 : 0;

			list[idx1].get(idx2).addScore(score[c]);
		}

		String ans = "";
		for(int i=1;i<5;i++) {
			list[i].sort((o1, o2) -> o1.score == o2.score ? o1.score - o2.score : o2.score - o1.score);
			ans += list[i].get(0).type;
		}

		return ans;
    }
}

 

728x90
250x250
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2025/02   »
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
글 보관함