[프로그래머스] Lv.1 성격 유형 검사하기 (Java)

subbni·2023년 1월 18일
0

프로그래머스

목록 보기
2/23
post-thumbnail

문제

나의 풀이

  • int[] score에 choices에 대한 점수 표현
  • hashMap<Character,Integer> 을 사용해서 각 유형(Character)에 대한 점수(Integer)를 저장
  • for(int i;i<survey.length;i++)를 돌면서 각 선택에 대한 점수 저장 처리
    = 4번 선택시 0점으로 아무 변화 없음
    = 1~3번 선택시 charAt(0) 문자에 점수 저장 처리
    = 5~7번 선택시 charAt(1) 문자에 점수 저장 처리
  • getResult() 메서드로 각 지표에서 더 높은 점수를 가진 유형을 뽑아 최종 성격 유형 String 만들기
import java.util.HashMap;

class Solution {
    public String solution(String[] survey, int[] choices) {
        int[] score = new int[] {3,2,1,0,1,2,3};
        HashMap<Character,Integer> map = new HashMap<>();

        for(int i=0;i<survey.length;i++) {
            if(choices[i]==4) continue;
            else if(choices[i]<4) {
                map.put(survey[i].charAt(0), map.getOrDefault(survey[i].charAt(0),0)
                +score[choices[i]-1]);
            } else if(choices[i]>4) {
                map.put(survey[i].charAt(1), map.getOrDefault(survey[i].charAt(1),0)
                +score[choices[i]-1]);
            }
        }
        
        return getResult(map);
    }

    String getResult(HashMap<Character,Integer> map) {
        char[][] index={{'R','T'},{'C','F'},{'J','M'},{'A','N'}};
        StringBuffer sb = new StringBuffer("");

        for(int i=0;i<index.length;i++) {
            if(map.getOrDefault(index[i][0], 0)>=map.getOrDefault(index[i][1], 0))  sb.append(index[i][0]);
            else sb.append(index[i][1]);
        }

        return sb.toString();
    }

}

후기

소요시간 : 33분

profile
개발콩나물

0개의 댓글