알고리즘 풀이 방법입니다.
문제(Problem) -> 생각(Think) -> 해결책(Solution) -> 리뷰(Review) 를 통해서 정리해서 작성합니다.
Problem📄

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

 

프로그래머스

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

programmers.co.kr


Think🤔

mbti 성격유형의 검사하는 문제

비동의를 누르면 앞에 있는 성격 유형에 + 점수

동의를 누르면 뒤에 있는 성격 유형에 + 점수

 

모르겠음의 점수는 4를 클릭했을 때이고, 이럴 경우 점수가 들어가지 않으므로 빠르게 끝냄

점수가 같을 경우 or 해당 유형의 질문이 없는 경우 A,B,C순 사전순으로 나열한다.

 

String으로 올릴경우 0~9까지 밖에 안되므로 pass

class Solution {
    public String solution(String[] survey, int[] choices) {
        String answer = "";
        String categoryList = "R0T0C0F0J0M0A0N0"; // 뒤에 점수
        int question = survey.length; // 질문지 갯수
        
        for(int i=0; i<question; i++){
            if(choices[i] == 4){
                continue;
            }else if(choices[i] < 4){
                survey[i].charAt(0);
            }
        }
        
        return answer;
    }
}
package kcue.test;

public class mbtiTeST {

	public static void main(String[] args) {

		String[] survey = { "AN", "CF", "MJ", "RT", "NA" };
//		String[] survey = { "TR", "RT", "TR" };
		
		int[] choices = { 5, 3, 2, 7, 5 };
//		int[] choices = { 7, 1, 3};

		String answer = "";
		String mbti = "RTCFJMAN";
		String mbtiCnt = "00000000";

		// 설문 조사 크기 만큼
		for (int i = 0; i < survey.length; i++) {
			int tmp = 0;
			int scoreCnt = 0;
			if (choices[i] == 4) {
				continue;
			} else if (choices[i] < 4) { // 비동의 앞에꺼 ++ 점수
				tmp = mbti.indexOf(survey[i].charAt(0)); // mbti에서 위치가 어디인지
				scoreCnt = mbtiCnt.charAt(tmp) + choices[i]; // 더 해진 값
			} else { // 동의 뒤에꺼 ++ 점수
				tmp = mbti.indexOf(survey[i].charAt(1));
				scoreCnt = mbtiCnt.charAt(tmp) + choices[i] - 4;
			}
			mbtiCnt = mbtiCnt.substring(0, tmp) + String.valueOf(scoreCnt) + mbtiCnt.substring(tmp + 1);
		}

		// 두개씩 비교
		for (int i = 0; i < 8; i += 2) {
			if (mbtiCnt.charAt(0) >= mbtiCnt.charAt(1)) {
				answer += mbti.charAt(i);
			} else {
				answer += mbti.charAt(i + 1);
			}
			System.out.println(answer + " : answer , " + i + " : i 값");
			// (mbtiCnt.charAt(0) >= mbtiCnt.charAt(1)) ? answer += mbti.charAt(i) : answer
			// += mbti.charAt(i+1);
		}
		System.out.println(answer);
	}

}

고민하다가 바꾼 방법들 .. String이 제일 편한 것 같다!

class Solution {
    public String solution(String[] survey, int[] choices) {
        String answer = "";
		String mbti = "RTCFJMAN";
		String mbtiCnt = "00000000";
		
		// 설문 조사 크기 만큼 
		for(int i=0; i<survey.length; i++){
			int tmp = 0;
			if(choices[i] == 4){
				continue;
			}else if(choices[i] < 4){
				//비동의
				tmp = mbti.indexOf(survey[0]);
				mbtiCnt.charAt(tmp) -= choices[i];
			}else{
				tmp = mbti.indexOf(survey[1]);
				mbtiCnt.charAt(tmp) += choices[i] - 4;
			}
		}
		
		// 두개씩 비교
		for(int i=0; i<8; i+=2){
			(mbtiCnt.charAt(0) >= mbtiCnt.charAt(1)) ? answer += mbti.charAt(i) : answer += mbti.charAt(i+1);  
		}
        
        return answer;
    }
}

class Solution {
    public String solution(String[] survey, int[] choices) {
        String answer = "";
        String categoryList = "R0T0C0F0J0M0A0N0"; // 뒤에 점수
        int question = survey.length; // 질문지 갯수
        
        for(int i=0; i<question; i++){
            if(choices[i] == 4){
                continue;
            }else if(choices[i] < 4){
				int tmp = categoryList.indexOf(survey[i].charAt(0));
				categoryList.charAt(tmp) -= choices[i];
            }else{
				int tmp = categoryList.indexOf(survey[i].charAt(0));
				categoryList.charAt(tmp) += choices[i] - 4; // 5,6,7 = 1,2,3 이기 때문
			}
        }
		
		// 점수가 큰 알파벳 부터
		// 배열을 두개를 하면 복잡하게 안해도 될 것 같은데 ..
		for()
        
        return answer;
    }
}







class Solution {
    public String solution(String[] survey, int[] choices) {
		HashMap<Character, Integer> hs = {{'R',0},{'T',0},{'C',0},{'F',0},{'J',0},{'M',0},{'A',0},{'N',0}};
		// Map.of('R',0); Java9 version 이상
		// Map.of를 사용하면 갯수 제한 있음
		hs.put('R',0);
		hs.put('T',0);
		hs.put('C',0);
		hs.put('F',0);
		hs.put('J',0);
		hs.put('M',0);
		hs.put('A',0);
		hs.put('N',0);
		
		for(Character i : choices){
			if(choices == 4){
				continue;
			}else if(choices < 4){
				hs.put(survey[0], hs.getOrDefault(survey[0],0) + choices);
			}else{
				hs.put(survey[1], hs.getOrDefault(survey[1],0) + choices);
			}
		}
		
		// 마지막에 어차피 꺼내서 다 비교해야함 ... 
		
        
        return answer;
    }
}

String으로 했을 경우 0~9까지 밖에 안돌아가므로 성격유형의 검사가 많은 경우 방법을 바꿔줘야 함..

R000T000 이런식으로 survey의 길이만큼 만들어줘야 함..! hashMap이 편할 것 같다


Solution✍
package kcue.test;

import java.util.HashMap;

public class mbtiHashMap {
	public static void main(String[] args) {
		String answer = "";
		
		String[] survey = { "AN", "CF", "MJ", "RT", "NA" };
//		String[] survey = { "TR", "RT", "TR" };
		
		int[] choices = { 5, 3, 2, 7, 5 };
//		int[] choices = { 7, 1, 3};
		
		HashMap<Character, Integer> hs = new HashMap<>();
		// Map.of('R',0); Java9 version 이상
		// Map.of를 사용하면 갯수 제한 있음
		hs.put('R',0);
		hs.put('T',0);
		hs.put('C',0);
		hs.put('F',0);
		hs.put('J',0);
		hs.put('M',0);
		hs.put('A',0);
		hs.put('N',0);
		
		for (int i = 0; i < survey.length; i++) {
			int cnt = 0;
			if (choices[i] == 4) {
				continue;
			} else if (choices[i] < 4) { 			  // 비동의 앞에꺼 ++ 점수
				cnt = hs.get(survey[i].charAt(0));
				hs.put(survey[i].charAt(0), cnt + 4 - choices[i]);
			} else { 								// 동의 뒤에꺼 ++ 점수
				cnt = hs.get(survey[i].charAt(1));
				hs.put(survey[i].charAt(1), cnt + choices[i] - 4);				
			}
		}
		
		answer += (hs.get('R') >= hs.get('T')) ? "R" : "T";
		answer += (hs.get('C') >= hs.get('F')) ? "C" : "F";
		answer += (hs.get('J') >= hs.get('M')) ? "J" : "M";
		answer += (hs.get('A') >= hs.get('N')) ? "A" : "N";
		
		System.out.println(answer);
	}
}

Review🤩

-


 

+ Recent posts