peacekim
할 수 있는 것과 할 수 없는 것.
peacekim
전체 방문자
오늘
어제
  • 분류 전체보기 (68)
    • 👨‍🏫ps (44)
      • ❄️프로그래머스 (20)
      • 🔟0️⃣백준 (21)
      • leetcode (3)
    • ✍🏻study (20)
      • 👐java (6)
      • 🍃spring (1)
      • 🥇algorithm (0)
      • 🚘oodp (4)
      • 📒 jpa (3)
      • 👣DB (2)
      • 🌂네트워크 (0)
      • 🎸기타 (3)
      • 👊 kotlin (1)
      • 🫥 jvm (0)
    • 📽project (4)
      • 🎀ReBoN (4)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

인기 글

최근 글

티스토리

hELLO · Designed By 정상우.
peacekim

할 수 있는 것과 할 수 없는 것.

[프로그래머스] 성격 유형 검사하기 [python]
👨‍🏫ps/❄️프로그래머스

[프로그래머스] 성격 유형 검사하기 [python]

2023. 3. 14. 20:48
반응형

문제

구현 문제였고, 다른 사람 풀이 보니 매우 쉽게 풀었다,, 객체를 사용하다보니 좀 더 오래 걸린 거 같지만 로직 자체는 간단하다.

시간 복잡도는 survery의 크기만큼 한번 for문을 돌아 O(N)이다

코드

def solution(survey, choices):
    answer = ''
    personalities = {}
    personalities["RT"] = Personality("R", "T")
    personalities["CF"] = Personality("C", "F")
    personalities["JM"] = Personality("J", "M")
    personalities["AN"] = Personality("A", "N")
    
    for idx in range(len(survey)):
        personality = personalities[survey[idx]] if survey[idx] in personalities else personalities[survey[idx][::-1]]
        left, right = survey[idx]
        
        score = choices[idx] - 4
        if score < 0 :
            personality.types[left] += abs(score)
        elif score > 0 :
            personality.types[right] += abs(score)
    for key, val in personalities.items():
        answer += val.getBiggerType()
    return answer

class Personality:
    def __init__(self, type1, type2):
        self.types = {}
        self.types[type1] = 0
        self.types[type2] = 0
    
    def getBiggerType(self):
        key1, key2 = self.types.keys()
        bigger = key1 if key1 < key2 else key2
        if self.types[key1] > self.types[key2]:
            bigger = key1
        elif self.types[key1] < self.types[key2]:
            bigger = key2
        return bigger
반응형

'👨‍🏫ps > ❄️프로그래머스' 카테고리의 다른 글

[프로그래머스] 게임 맵 최단거리  (0) 2023.03.16
[프로그래머스] 숫자의 표현 [python]  (0) 2023.03.15
[프로그래머스] 신규 아이디 추천 [python]  (0) 2023.03.14
[프로그래머스] 신고 결과 받기 [python]  (0) 2023.03.13
[프로그래머스] 개인정보 수집 유효기간 [Python]  (0) 2023.03.12
    '👨‍🏫ps/❄️프로그래머스' 카테고리의 다른 글
    • [프로그래머스] 숫자의 표현 [python]
    • [프로그래머스] 신규 아이디 추천 [python]
    • [프로그래머스] 신고 결과 받기 [python]
    • [프로그래머스] 개인정보 수집 유효기간 [Python]
    peacekim
    peacekim
    할 수 있는 것과 할 수 없는 것. github: https://github.com/PyeongGangKim

    티스토리툴바