본문 바로가기

알고리즘/구현

프로그래머스 - 성격 유형 검사하기(Kotlin)

본 알고리즘 풀이는 Routine Study에서 진행하고 있습니다.

https://github.com/ROUTINE-STUDY/Algorithm

 

저를 포함한 구성원이 대부분 초보이므로, 원하시는분은 언제라도 들어오셔도 좋습니다.

 

GitHub - ROUTINE-STUDY/Algorithm: 초보 알고리즘 스터디 / 누구나 참여 가능

초보 알고리즘 스터디 / 누구나 참여 가능 :runner:. Contribute to ROUTINE-STUDY/Algorithm development by creating an account on GitHub.

github.com

문의는 댓글 바람.

 

문제 출처 : https://school.programmers.co.kr/learn/courses/30/lessons/118666?language=kotlin

 

[문제 설명]

시험을 통해 나온 점수를 이용하여 성격을 정하시오.

[접근 방법]

문제 조건에 주어진 대로 각 성격에 대해여 점수를 메기고, 그걸로 성격을 정하면 된다.

 

주의해야 할 예외는 

단, 한 문제도 해당 성격에 대해 물어보지 않는 경우.

점수가 같은 경우이다.

 

또한, choice에 따라 점수가 달라지는 것도 조심해라.

import kotlin.math.absoluteValue

class Solution {
    fun solution(survey: Array<String>, choices: IntArray): String {
        var answer = StringBuilder()
        val size = survey.size
        val map = HashMap<Char, Int>()

        for (i in 0 until size) {
            if (choices[i] == 4) {
                continue
            }

            if (choices[i] < 4) {
                map[survey[i][0]] =  (map[survey[i][0]] ?: 0) + (choices[i] - 4).absoluteValue
            } else {
                map[survey[i][1]] =  (map[survey[i][1]] ?: 0) + (choices[i] - 4).absoluteValue
            }
        }

        answer.append(chooseCharacter('R', 'T', map))
        answer.append(chooseCharacter('C', 'F', map))
        answer.append(chooseCharacter('J', 'M', map))
        answer.append(chooseCharacter('A', 'N', map))
        return answer.toString()
    }
}

fun chooseCharacter(characterA: Char, characterB: Char, map: HashMap<Char, Int>): Char {
    return if (map[characterA] ?: 0 >= map[characterB] ?: 0) {
        characterA
    } else {
        characterB
    }
}

 

728x90
반응형

'알고리즘 > 구현' 카테고리의 다른 글

17780번: 새로운 게임 (Kotlin)  (0) 2023.12.19
백준 - 뱀(Kotlin)  (1) 2022.10.03
백준 - 1347 미로 만들기(Kotlin)  (0) 2022.08.23
백준 - 사람의 수(Kotlin)  (0) 2022.08.03
백준 - 수 정렬하기2(Kotlin)  (0) 2022.07.14